Calculating the Moving Average of a List

后端 未结 18 1179
悲哀的现实
悲哀的现实 2020-12-07 09:01

This weekend I decided to try my hand at some Scala and Clojure. I\'m proficient with object oriented programming, and so Scala was easy to pick up as a language, but wante

18条回答
  •  孤城傲影
    2020-12-07 09:36

    Here is another (functional) Clojure solution:

    (defn avarage [coll]
      (/ (reduce + coll)
         (count coll)))
    
    (defn ma [period coll]
      (map avarage (partition period 1 coll)))
    

    The zeros at the beginning of the sequence must still be added if that is a requirement.

提交回复
热议问题