core: move comparator/reductions/tree-seq to overlay (+ spec tests)
Sixth pure-fn batch (jolt-1j0 phase 2), 3 leaf fns, now with regression tests per the per-batch workflow. reductions is the canonical form, so (reductions f []) calls (f) -> [0] instead of the prior []; tree-seq is eager pre-order DFS. conformance 228/228 x3, clojure-test-suite 3929, full suite green, bench A/B flat.
This commit is contained in:
parent
c35cf7bdbc
commit
921b395dfd
3 changed files with 33 additions and 29 deletions
|
|
@ -143,3 +143,28 @@
|
|||
(defn undefined? [x] false)
|
||||
|
||||
(defn keyword-identical? [a b] (= a b))
|
||||
|
||||
(defn comparator [pred]
|
||||
(fn [a b] (cond (pred a b) -1 (pred b a) 1 :else 0)))
|
||||
|
||||
;; Eager (Jolt has no laziness yet): a vector of the running accumulators.
|
||||
(defn reductions
|
||||
([f coll]
|
||||
(let [s (seq coll)]
|
||||
(if s
|
||||
(reductions f (first s) (rest s))
|
||||
(list (f)))))
|
||||
([f init coll]
|
||||
(loop [acc init xs (seq coll) out [init]]
|
||||
(if xs
|
||||
(let [a (f acc (first xs))] (recur a (next xs) (conj out a)))
|
||||
out))))
|
||||
|
||||
;; Eager pre-order DFS (Clojure's is lazy; same order, fully realized here).
|
||||
(defn tree-seq [branch? children root]
|
||||
(let [walk (fn walk [acc node]
|
||||
(let [acc (conj acc node)]
|
||||
(if (branch? node)
|
||||
(reduce walk acc (children node))
|
||||
acc)))]
|
||||
(walk [] root)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue