I have collection of lists and I want to apply "reduce +" for each list in collection. I think I should combine "apply", "map" and "reduce +", but I can't understand how.
Example:
[[1 2 3] [4 5 3] [2 5 1]] => [6 12 8]
No need for apply
. map
and reduce
will work fine:
(map (partial reduce +) [[1 2 3] [4 5 3] [2 5 1]])
map
will call the function on each member of the list and partial
simply creates a 'curried' version of reduce that expects one parameter. it could also be written like #(reduce + %)
or (fn [lst] (reduce + lst))
Update
You could actually use apply in place of reduce here as well (just not both):
(map (partial apply +) [[1 2 3] [4 5 3] [2 5 1]])
Further Update
If you have any performance concerns, see the comments on this answer for some great tips by @AlexMiller