Step 5 final: dedupe make-lazy-seq, trampoline ifn?, rand-int native

dedupe: use make-lazy-seq + coll->cells directly — lazy-seq
macro is not available at 20-coll tier (defined in 30-macros.clj).

trampoline: use ifn? instead of fn? (not available), fix
single-arity to call (f) directly instead of recursive (trampoline f (f)).

rand-int: removed from overlay, stays native in core.janet
(math/floor and math/random not available in Clojure namespace).
This commit is contained in:
Yogthos 2026-06-08 11:09:59 -04:00
parent 64b1c60939
commit 48ce42d94d

View file

@ -210,29 +210,35 @@
;; trampoline: repeatedly calls f with args until a non-function result.
(defn trampoline
([f] (trampoline f (f)))
([f]
(let [ret (f)]
(if (ifn? ret)
(recur ret)
ret)))
([f & args]
(let [ret (apply f args)]
(if (fn? ret)
(if (ifn? 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 [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))))))))]
(make-lazy-seq
(fn* []
(let [s (seq s)]
(if s
(let [x (first s)]
(if (= x prev)
(coll->cells (step (rest s) prev))
(coll->cells (cons x (step (rest s) x)))))
nil)))))]
(let [s (seq coll)]
(if s
(cons (first s) (step (rest s) (first s)))
(make-lazy-seq
(fn* [] (coll->cells (cons (first s) (step (rest s) (first s))))))
()))))
;; Internal helper for {:keys [...]} destructuring over a seq of k/v pairs: