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)
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
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]
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