Re: Moving average from Java to Clojure

2014-07-20 Thread Jony Hudson
On Sunday, 20 July 2014 12:48:19 UTC+1, Cecil Westerhof wrote: > or is there a better way? > Probably not the answer you're looking for, but the exponentially-weighted moving average doesn't require any state other than the current value: (defn ewma [alpha] (fn [avg new] (+ (* (- 1 alpha) avg)

Re: Moving average from Java to Clojure

2014-07-20 Thread Mike Fikes
Hey Cecil, In addition to using peek instead of first, as indicated by Plinio, the moving-average function above uses some poor names, in hindsight, especially the "old-queue" parameter name. I'd suggest naming it queue, as it refers to an atom. You could even consider naming the function mov

Re: Moving average from Java to Clojure

2014-07-20 Thread Mike Fikes
There actually is a queue implementation. Here is a way to use it for your problem: (defn make-moving-average-queue [n] (atom {:lengthn :current-total 0.0 :old-values(clojure.lang.PersistentQueue/EMPTY)})) (defn update-moving-average-queue [old-queue next-value]

Re: Moving average from Java to Clojure

2014-07-20 Thread PlĂ­nio Balduino
Hi Cecil Clojure has a clojure.lang.PersistentQueue to work with queues. I don't know why it doesn't have a reader macro, but works fine and returns you a Clojure collection you can handle with cons and peek. You can start with an empty queue with clojure.lang.PersistentQueue/EMPTY or insert y