Sum is just about the only case where reduce is useful, but in reality it should never be passed around like that. Why keep an add function around that is only meant to be used together with a reduce? Just wrap it once and never use reduce again:
add
reduce
const sum = (array) => array.reduce((a, b) => a + b); sum([1, 2, 3]) //=> 6
Don't tell me arr.reduce(add) makes more sense than sum(arr) because it doesn't.
arr.reduce(add)
sum(arr)
In reality you can write sum event more efficiently with a regular loop and every part of your code benefits.
sum
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Sum is just about the only case where reduce is useful, but in reality it should never be passed around like that. Why keep an
add
function around that is only meant to be used together with areduce
? Just wrap it once and never use reduce again:Don't tell me
arr.reduce(add)
makes more sense thansum(arr)
because it doesn't.In reality you can write
sum
event more efficiently with a regular loop and every part of your code benefits.