On 2010 Mar 23, at 8:26 AM, Meikel Brandmeyer wrote:
here some notes:

Thanks!

I would use vector instead of list in the example since vector is more
idiomatic in Clojure.

Ok. Just my old lisp roots showing. Still haven't gotten the fingers/ spine
rewired to use [] around defn parameters. :)


   (if (seq seq1)
       (lazy-seq

Use when-let (more idiomatic) and move lazy-seq outside to achieve
full laziness. Otherwise realisation of the seq is always one step
ahead.

Thanks. I wasn't sure how high to lift the lazy-seq. Still annoying that
(if seq1 ...) and (if (seq seq1) ...) aren't the same. Oh well, that'll take
some repetition to get into the reflexes as well.



         (let [[s1 & s1tail]   seq1

Don't use this destructuring in this case, because it forces again the
realisation of the seq one step ahead.

If I need s1 anyways, what is the "one step ahead part" that you refer to? That is just my translation of the Haskell idiom. I'm sad to see that there
isn't a parallel clojure idiom with the lazy semantics.


So here is my try:

(defn semi-map
  "Lazy sequence of seq1, optionally combined with seq2.
   When (pred (first seq1)) item is true, yield
   (fun (first seq1) (first seq2)) otherwise yield (first seq1)
   without advancing seq2.
   Example:
      (semi-map vowel? vector \"Hellow Word\" (iterate inc 1))
   -> (\\H [\\e 1] \\l \\l [\\o 2] \\w \\space \\W [\\o 3] \\r \\d)"
  [pred f seq1 seq2]
  (lazy-seq
    (when-let [seq1 (seq seq1)]
      (let [fst1 (first seq1)]
        (if (pred fst1)
          (cons (f fst1 (first seq2))
                (semi-map pred f (rest seq1) (rest seq2)))
          (cons fst1 (semi-map pred f (rest seq1) seq2)))))))

Thanks!

-Doug

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or 
reply to this email with the words "REMOVE ME" as the subject.

Reply via email to