Skip to content

Commit

Permalink
[nop] Use new enc/reduce-multi
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Sep 7, 2023
1 parent e926009 commit f8eeb1b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 41 deletions.
39 changes: 1 addition & 38 deletions src/taoensso/tufte/stats.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,6 @@
#?(:clj (let [c (Class/forName "[J")] (defn longs? "Returns true iff given long array" [x] (instance? c x))))
#?(:clj (let [c (Class/forName "[D")] (defn doubles? "Returns true iff given double array" [x] (instance? c x))))

;;;; Tuples

(do
(deftype Tup2 [x y ])
(deftype Tup3 [x y z]))

(defn multi-reduce
"Like `reduce` but supports separate simultaneous accumulators
as a micro-optimisation when reducing a large collection multiple
times."
;; Faster than using volatiles
([f init coll] (reduce f init coll))
([f1 init1 f2 init2 coll]
(let [^Tup2 tuple
(reduce
(fn [^Tup2 tuple in]
(Tup2.
(f1 (.-x tuple) in)
(f2 (.-y tuple) in)))
(Tup2. init1 init2)
coll)]

[(.-x tuple) (.-y tuple)]))

([f1 init1 f2 init2 f3 init3 coll]
(let [^Tup3 tuple
(reduce
(fn [^Tup3 tuple in]
(Tup3.
(f1 (.-x tuple) in)
(f2 (.-y tuple) in)
(f2 (.-z tuple) in)))
(Tup3. init1 init2 init3)
coll)]

[(.-x tuple) (.-y tuple) (.-z tuple)])))

;;;; Sorted nums

(deftype SortedLongs [^longs a]
Expand Down Expand Up @@ -360,7 +323,7 @@
xsum (double (reduce rf-sum 0.0 snums))
xbar (/ xsum nx)
[^double xvar-sum ^double xmad-sum]
(multi-reduce
(enc/reduce-multi
(partial rf-sum-variance xbar) 0.0
(partial rf-sum-abs-deviation xbar) 0.0
snums)]
Expand Down
3 changes: 0 additions & 3 deletions test/taoensso/stats_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
(is (= (vec (stats/sorted-nums [1.0 3 2])) [1.0 2.0 3.0]))
(is (= (vec (stats/sorted-nums [1 3.0 2.0])) [1 2 3]))])])

(deftest multi-reduce
[(is (= (stats/multi-reduce + 0 - 0 (range 1e4)) [49995000 -49995000]))])

(deftest weighted-nth
[(is (= (#'stats/weighted-nth 0.5 [1 3]) 2.0))
(is (= (#'stats/weighted-nth 0.5 [1 10]) 5.5))
Expand Down

0 comments on commit f8eeb1b

Please sign in to comment.