Remember the one-liner I gave you last time? (defn indexed-pred [pred coll] (map #(if (pred %1) [%2 %1] [%1]) coll (reductions + 0 (map #(if (pred %) 1 0) coll))))
Here's how little it has to change: (defn funkymonkey [pred src coll] (map #(if (pred %1) [(first %2) %1] [%1]) coll (reductions #(if (pred %2) (rest %1) %1) src coll))) -Per On Tue, Mar 23, 2010 at 6:53 PM, Douglas Philips <d...@mac.com> wrote: > 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. > -- 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.