On Feb 19, 2014, at 12:28 AM, Laurent Droin <[email protected]> wrote:
> Ah, thank you. "interleave" is what I was looking for. I looked for "weave",
> "zip", "map", "concat", and all the "see also" but did not find "interleave".
> Interleave will of course not handle the last value in the categories
> collection so my first instinct will be to call (into [] ) on the map
> returned by interleave, and to add (using conj) the (last categories) to the
> vector. Not sure whether there is a better way.
Here's an interleave-all function that's like interleave but uses all the
elements of all the collections. Be careful if any of your collections might
be infinite. In that case, you need to wrap something like `take` around the
call.
(defn interleave-all
"Returns a lazy seq of the first item in each collection, then the second,
etc. If one
collection ends, continues to interleave the others. Naturally, you should
take care with
infinite sequences."
([] (lazy-seq nil))
([c] (lazy-seq c))
([c1 c2]
(lazy-seq
(cond (not (seq c1)) c2
(not (seq c2)) c1
:else (conj (interleave-all (rest c1) (rest c2)) (first c2) (first
c1)))))
([c1 c2 & colls]
(lazy-seq
(let [ss (keep seq (conj colls c2 c1))]
(concat (map first ss) (apply interleave-all (map rest ss)))))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.