If you’re interested in a transducer version of partition-when, here’s my code
from https://github.com/miner/transmuters
;; collection version by Frank on mailing list
;; SEM added transducer version, adapted from partition-by
(defn partition-when
"Applies f to each value in coll, starting a new partition each time f
returns a
true value. Returns a lazy seq of partitions. Returns a stateful
transducer when no collection is provided."
{:static true}
([f]
(fn [rf]
(let [a (java.util.ArrayList.)]
(fn
([] (rf))
([result]
(let [result (if (.isEmpty a)
result
(let [v (vec (.toArray a))]
;;clear first!
(.clear a)
(unreduced (rf result v))))]
(rf result)))
([result input]
(if (.isEmpty a)
(do (.add a input)
result)
(if (f input)
(let [v (vec (.toArray a))]
(.clear a)
(let [ret (rf result v)]
(when-not (reduced? ret)
(.add a input))
ret))
(do
(.add a input)
result))))))))
([f coll]
(lazy-seq
(when-let [s (seq coll)]
(let [fst (first s)
run (cons fst (take-while #(not (f %)) (next s)))]
(cons run (partition-when f (seq (drop (count run) s)))))))))
--
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/d/optout.