How would I calculate the average of a list of numbers using map and reduce.
Ideally I want to call reduce on a list and get an average back. You may optionally map and filter that list first.
A skeleton LISP attempt:
(defun average (list)
(reduce ... list))
A skeleton JS attempt:
function average (array) {
return array.reduce(function() {
..
}, 0);
}
If you post an answer with actual code in a language please explain it as if I'm a beginner in that langauge (which I probably will be).
I want to avoid the trivial answer of
function average (array) {
return sum(array) / array.length;
}
This uses division at the end rather then a reduce statement. I consider this "cheating".
[[Edit]]
Solved my own problem. If anyone has an elegant solution using syntactic sugar from LISP or Haskell I would be interested.