core: jolt-cru — partition-all/partition-by transducer (1-arg) arities
(partition-all n) and (partition-by f) now return stateful transducers, so they compose with into/sequence/transduce/comp (previously errored "called with 1 argument, expected 2"). Both buffer a window, emit on boundary, flush a partial trailing window in the completion arity, and honor `reduced` (early stop drops the in-progress window, no extra trailing partition) — matching Clojure. partition-all: td-partition-all in core.janet (Janet, alongside the other td-*), core-partition-all made variadic. partition-by: transducer arity added to the overlay defn in Clojure with volatiles (stays with its lazy collection arity). Gate: conformance 253x3 (+4 transducer cases incl comp + reduced), lazy-infinite 44/44, fixpoint, self-host, specs+unit green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c7378f4be0
commit
a68210e440
3 changed files with 68 additions and 13 deletions
|
|
@ -23,14 +23,39 @@
|
|||
(recur (conj ret (first s)) (next s))
|
||||
(seq ret))))
|
||||
|
||||
;; Lazy partition-by: groups consecutive elements by (f x), matching Clojure/CLJS.
|
||||
(defn partition-by [f coll]
|
||||
(let [step (fn step [s]
|
||||
(lazy-seq
|
||||
(let [s (seq s)]
|
||||
(when s
|
||||
(let [fst (first s)
|
||||
fv (f fst)
|
||||
run (cons fst (take-while (fn [x] (= fv (f x))) (rest s)))]
|
||||
(cons run (step (lazy-seq (drop (count run) s)))))))))]
|
||||
(step coll)))
|
||||
;; partition-by: (partition-by f) is a stateful transducer (buffer a run, emit on
|
||||
;; key change, flush on completion — via volatiles, matching Clojure); (partition-by
|
||||
;; f coll) is the lazy collection arity.
|
||||
(defn partition-by
|
||||
([f]
|
||||
(fn [rf]
|
||||
(let [buf (volatile! [])
|
||||
pv (volatile! nil)
|
||||
started (volatile! false)]
|
||||
(fn
|
||||
([] (rf))
|
||||
([result]
|
||||
(let [b @buf
|
||||
result (if (zero? (count b))
|
||||
result
|
||||
(do (vreset! buf []) (unreduced (rf result b))))]
|
||||
(rf result)))
|
||||
([result input]
|
||||
(let [val (f input)]
|
||||
(if (or (not @started) (= val @pv))
|
||||
(do (vreset! started true) (vreset! pv val) (vswap! buf conj input) result)
|
||||
(let [b @buf]
|
||||
(vreset! buf []) (vreset! pv val)
|
||||
(let [ret (rf result b)]
|
||||
(when-not (reduced? ret) (vswap! buf conj input))
|
||||
ret)))))))))
|
||||
([f coll]
|
||||
(let [step (fn step [s]
|
||||
(lazy-seq
|
||||
(let [s (seq s)]
|
||||
(when s
|
||||
(let [fst (first s)
|
||||
fv (f fst)
|
||||
run (cons fst (take-while (fn [x] (= fv (f x))) (rest s)))]
|
||||
(cons run (step (lazy-seq (drop (count run) s)))))))))]
|
||||
(step coll))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue