From bae87d1b50560e5b21c0a1a17ca466021f4d30ab Mon Sep 17 00:00:00 2001 From: Christophe Grand Date: Wed, 13 Jun 2018 18:08:34 +0200 Subject: [PATCH] x/avg and rfs/avg return `nil` when no input --- src/net/cgrand/xforms/rfs.cljc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/net/cgrand/xforms/rfs.cljc b/src/net/cgrand/xforms/rfs.cljc index 35e95d6..4a18a85 100644 --- a/src/net/cgrand/xforms/rfs.cljc +++ b/src/net/cgrand/xforms/rfs.cljc @@ -55,11 +55,13 @@ (defn avg "Reducing fn to compute the arithmetic mean." - ([] (transient [0 0])) - ([[n sum]] (/ sum n)) + ([] nil) + ([^doubles acc] (when acc (/ (aget acc 1) (aget acc 0)))) ([acc x] (avg acc x 1)) - ([[n sum :as acc] x w] ; weighted mean - (-> acc (assoc! 0 (+ n w)) (assoc! 1 (+ sum (* w x)))))) + ([^doubles acc x w] ; weighted mean + (doto (or acc #?(:clj (double-array 3) :cljs #js [0.0 0.0])) + (aset 0 (+ (aget acc 0) w)) + (aset 1 (+ (aget acc 1) (* w x)))))) (defn sd "Reducing fn to compute the standard deviation. Returns 0 if no or only one item."