Step 5: partition-by, dedupe → lazy overlay + trampoline/rand-int overlay
partition-by (10-seq.clj): ported canonical lazy implementation from CLJS — lazy-seq + cons + take-while, matching Clojure/CLJS exactly. Fixes the third and final remaining leak from Step 3. dedupe (20-coll.clj): replaced eager vec-based impl with lazy step function using lazy-seq + cons. Infinite input no longer hangs. trampoline (20-coll.clj): pure HOF ported from CLJS — recur until non-function result. Removed core-trampoline + binding from Janet. rand-int (20-coll.clj): thin overlay over Janet math/random + math/floor. Removed core-rand-int + binding from Janet. Removed core-partition-by + binding from core.janet (now in overlay). Tests: added dedupe infinite-input case to lazy-infinite harness. 22/22 pass. Conformance 229x3. Specs 32/32.
This commit is contained in:
parent
42da5cef9a
commit
64b1c60939
4 changed files with 37 additions and 31 deletions
|
|
@ -208,16 +208,32 @@
|
|||
;; No ratio type on Jolt, so rationalize is identity.
|
||||
(defn rationalize [x] x)
|
||||
|
||||
;; trampoline: repeatedly calls f with args until a non-function result.
|
||||
(defn trampoline
|
||||
([f] (trampoline f (f)))
|
||||
([f & args]
|
||||
(let [ret (apply f args)]
|
||||
(if (fn? ret)
|
||||
(recur ret)
|
||||
ret))))
|
||||
|
||||
;; rand-int: random integer in [0, n). Uses Janet math/random.
|
||||
(defn rand-int [n] (math/floor (* (math/random) n)))
|
||||
|
||||
;; Eager dedupe of consecutive equal elements (Jolt has no transducer arity yet).
|
||||
(defn dedupe [coll]
|
||||
(let [c (vec coll)]
|
||||
(if (empty? c)
|
||||
[]
|
||||
(loop [prev (first c) xs (rest c) out [(first c)]]
|
||||
(if (seq xs)
|
||||
(let [x (first xs)]
|
||||
(recur x (rest xs) (if (= x prev) out (conj out x))))
|
||||
out)))))
|
||||
(let [step (fn step [s prev]
|
||||
(lazy-seq
|
||||
(let [s (seq s)]
|
||||
(when s
|
||||
(let [x (first s)]
|
||||
(if (= x prev)
|
||||
(step (rest s) prev)
|
||||
(cons x (step (rest s) x))))))))]
|
||||
(let [s (seq coll)]
|
||||
(if s
|
||||
(cons (first s) (step (rest s) (first s)))
|
||||
()))))
|
||||
|
||||
;; Internal helper for {:keys [...]} destructuring over a seq of k/v pairs:
|
||||
;; builds a map from consecutive pairs, dropping a trailing unpaired element.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue