Last week, Per Vognsen answered my first version of this question, how
can I number just the vowels in a string: http://groups.google.com/group/clojure/msg/22186113b36041f1?hl=en
But that got me thinking that "numbering" was just an instance of
consuming from the (iterate inc 0) sequence.
As I am fleshing out more of my program, I find it appealing to
abstract out this pattern of using
a predict on one sequence to control the consumption of and
combination with another sequence.
Here is my more general version (in the API sense) of that concept.
Names are hard and I don't particularly like semi-map but I like it
better than all the other names I've come up with so far. Hopefully
this message will be sent/read with a fixed-width font. Any and all
criticisms, suggestions, improvements welcome. (I'm happy to use this
as is, but as in my first message, I have this nagging feeling that it
could be simpler or more idiomatic)
Thanks,
-Doug
(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? list \"Hellow Word\" (iterate inc 1))
-> (\\H (\\e 1) \\l \\l (\\o 2) \\w \\space \\W (\\o 3) \\r \\d)"
[pred fun seq1 seq2]
(if (seq seq1)
(lazy-seq
(let [[s1 & s1tail] seq1
pred? (pred s1)
[news1 newseq2] (if pred? [(fun s1 (first seq2)) (rest
seq2)]
[s1 seq2])]
(cons news1 (xyzzy pred fun s1tail newseq2))))))
--
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.