Hi,
Am 08.08.2010 um 05:27 schrieb David Cabana:
> Using a vector instead of a list as the accumulator makes it possible
> to skip the mapping of reverse used in the earlier version of pw:
>
> (defn pw [f? x]
> (let [phi (fn [a e]
> (if (f? e)
> (cons [e] a )
> (cons (conj (first a) e)
> (rest a))))]
> (reverse
> (reduce phi [] x))))
Just for fun another low-level solution:
(defn partition-when
[pred coll]
(let [step (fn [p s]
(if s
(let [fst (first s)]
(if (pred fst)
[p s]
(recur (conj p fst) (next s))))
[p nil]))]
(lazy-seq
(when-let [s (seq coll)]
(let [[p r] (step [(first s)] (next s))]
(cons p (partition-when pred r)))))))
Hopefully not holding onto the head, as lazy as it can get and working with
infinite input.
Sincerely
Meikel
--
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