problem: convert a collection [1 2 0 1 2 3 0 1 2 3 0 0 1 2] into
partitions like:
((1 2) (0 1 2 3) (0 1 2 3) (0) (0 1 2))
In this case, start each partition on a 0.
I looked at the various partition functions but none of them would do
the trick without adding unnecessary complexity. Instead I wrote a
new function based on partition-by:
Solution:
(defn partition-starting-every
"Partition the sequence starting each partition when the f is true."
[f coll]
(if-let [coll (seq coll)]
(let [p (cons (first coll) (take-while (complement f) (rest
coll)))]
(lazy-seq (cons p (partition-starting-every f (drop (count p)
coll)))))))
user=>(partition-starting-every zero? [1 2 0 1 2 3 0 1 2 3 0 0 1 2])
((1 2) (0 1 2 3) (0 1 2 3) (0) (0 1 2))
Questions:
1 - Is there a simpler way to do this using existing partition
functions?
2 - If not, is this something people are interested in having
contributed?
In looking at the partition functions they are very similar. Maybe
there is a why to combine them in to a single function.
--
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