function | since v0.0-927 | clojure.core/partition-by | Edit |
(partition-by f)
(partition-by f coll)
Applies f
to each value in coll
, splitting it each time f
returns a new
value. Returns a lazy sequence of partitions.
Returns a stateful transducer when no collection is provided.
Applies f to each value in coll, splitting it each time f returns a new value. Returns a lazy seq of partitions. Returns a stateful transducer when no collection is provided.
(defn partition-by
([f]
(fn [rf]
(let [a (array-list)
pa (volatile! ::none)]
(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]
(let [pval @pa
val (f input)]
(vreset! pa val)
(if (or (keyword-identical? pval ::none)
(= val pval))
(do
(.add a input)
result)
(let [v (vec (.toArray a))]
(.clear a)
(let [ret (rf result v)]
(when-not (reduced? ret)
(.add a input))
ret)))))))))
([f coll]
(lazy-seq
(when-let [s (seq coll)]
(let [fst (first s)
fv (f fst)
run (cons fst (take-while #(= fv (f %)) (next s)))]
(cons run (partition-by f (lazy-seq (drop (count run) s)))))))))