RosettaCodeData/Task/Cumulative-standard-deviation/Clojure/cumulative-standard-deviation.clj

10 lines
372 B
Clojure
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
(defn stateful-std-deviation[x]
(letfn [(std-dev[x]
(let [v (deref (find-var (symbol (str *ns* "/v"))))]
(swap! v conj x)
(let [m (/ (reduce + @v) (count @v))]
(Math/sqrt (/ (reduce + (map #(* (- m %) (- m %)) @v)) (count @v))))))]
(when (nil? (resolve 'v))
(intern *ns* 'v (atom [])))
(std-dev x)))