Calculating the Moving Average of a List

后端 未结 18 1184
悲哀的现实
悲哀的现实 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:50

    In Haskell pseudocode:

    group4 (a:b:c:d:xs) = [a,b,c,d] : group4 (b:c:d:xs)
    group4 _ = []
    
    avg4 xs = sum xs / 4
    
    running4avg nums = (map avg4 (group4 nums))
    

    or pointfree

    runnig4avg = map avg4 . group4
    

    (Now one really should abstract the 4 out ....)

提交回复
热议问题