On Wed, Apr 22, 2009 at 8:03 PM, Meikel Brandmeyer <m...@kotka.de> wrote: > Hi, > > Am 22.04.2009 um 16:57 schrieb samppi: > >> Let's say I have a sequence of integers: >> (def a (3 9 1 5 102 -322 ...)) >> >> Is there a function for inserting an object—let's say :foo—after >> elements that fulfill a certain predicate? >> Furthermore, I mean inserting :foo after any block of elements that >> fulfill it: >> >> (mystery-function (partial > 6) a) ; -> (3 :foo 9 1 5 :foo 102 >> -322 :foo ...) >> >> Is it possible to do this without a loop? > > (defn mystery-function > [f o s] > (when-let [s (seq s)] > (lazy-seq > (let [fst (first s)] > (concat [fst] > (when (f fst) [o]) > (mystery-function f o (rest s)))))))
Your version gives the same answer as mine, but I believe what he wants is something that skips over all the elements that pass the test and only inserts one instance of o after them. That's why in his example there is not a :foo after 1, but only after 5. Laurent's version gets this right. -- Michael Wood <esiot...@gmail.com> --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---