Hi,
Here is my final word (I promise :-):
(defn mystery-function [f s o]
(let [s (seq s)
preds (map f s)]
(mapcat #(if (and %2 (not %3)) [%1 o] [%1]) s preds
(concat (rest preds) [false]))))
I has only one drawback AFAIK (and depending on your use case, it may
or may not be a problem to you): it is a little bit more eager than
the pure lazy-seq version of Meikel (the combination of mapcat and
(rest preds) makes it consume s 3 to 4 elements (don't remember) ahead
of the result of mystery-function (but it's a constant).
--
Laurent
2009/4/22 samppi <[email protected]>:
>
> 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?
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---