So far, our moving-average
function only gives us a partitioned list of prices (or ticks). Now, we only need to visit a sliced list at each increment and calculate each increment's price average. The reduce
function provides a way of accumulating (or folding over) each item in a list and producing a result based on those values. We'll take our partitioned list and have reduce
apply an accumulator function on each sublist. The form looks roughly like this:
(reduce (fn [accumulated-result-so-far each-item-list] ;; ** single line comments come after semi-colons ;; do stuff, return accumulated result ;; for each item iteration ) initial-list-of-accumulated-values initial-list-of-input-values)
Our accumulator function only has to maintain a few temporary values that our equation needs. In this case, it's the sum of each-item-list
(or lazy sequence). For this, we want to contain or bind intermediate values in a temporary...