Re: Efficient moving average

2011-05-09 Thread Ken Wesson
On Mon, May 9, 2011 at 6:55 PM, Andy Fingerhut wrote: > (ns movavg >  (:gen-class)) > > (set! *warn-on-reflection* true) > > > (defn sliding-window-moving-average [window lst] >  (let [w (int window) >        w-1 (int (dec w))] >    (loop [rolling-sum (apply + (take w lst)) >           last-w (obj

Re: Efficient moving average

2011-05-09 Thread Andy Fingerhut
On May 9, 2011, at 6:45 AM, Andreas Kostler wrote: > Hi all, > I'm trying to calculate the moving average of a certain window size. > One straight forward approach is: > (defn lazy-avg [coll] > (let [[sum cnt] (reduce > (fn [[v c] val] [(+ val v) (inc c)]) > [

Efficient moving average

2011-05-09 Thread Andreas Kostler
Hi all, I'm trying to calculate the moving average of a certain window size. One straight forward approach is: (defn lazy-avg [coll] (let [[sum cnt] (reduce (fn [[v c] val] [(+ val v) (inc c)]) [0 0] coll)] (if (zero? cnt) 0 (/ sum cnt)