3 ms·
That is why learning programming languages is important. I would argue this concept is picked from functional ones. `reduce` function is not hard to read if yo
by perceptronas 7y ago
That is why learning programming languages is important. I would argue this concept is picked from functional ones. `reduce` function is not hard to read if you understand how it works and code is a lot easier to read because it does not introduce mutability. Even better - it has almost the same signature in other languages so you only need to learn it once.
- codedokode 7y agoI always forget the order of arguments and what they mean. For me it is an awful function that should not exist.
- deleted 7y ago[deleted]
- beatgammit 7y agoIsn't the solution there to name variables well? A reduce function turns an array into a single value, passing the result of computation of each element to the next element. So, the signature is: function (accumulator, element) -> accumulator So, you take in some accumulated value (or the initial value on the first iteration) and an element and return the accumulated value to the next element. If the values were swapped, it would be inconsistent with the rest of the array functions where optional arguments are tacked on to the end (e.g. index, array), so the obvious assumption is that the accumulator goes in front to keep the rest of it as similar to the other array functions as possible. I think it's unfortunate that array.reduce takes the accumulator after the function, but that also makes sense since it's technically valid to omit the accumulator (defaults to undefined). The problem, I think, is that reduce is often abused when another function might make more sense (e.g. as a ghetto "find" or something). If it's used properly, I think it's quite elegant. If it's not, it's a confusing mess.
- edflsafoiewq 7y agoThe accumulator goes on the left if you're folding left and on the right if you're folding right.
- perceptronas 7y agotry implementing it, you only need to learn it once
- Barrin92 7y agoI don't know if you looked at the example but the example given has two anonymous function inside the reduce and returns things(???), it's unreadable to me and I'm very comfortable reading functional code, but I don't know what the hell the example is supposed to be
- azangru 7y ago> but the example given has two anonymous function inside the reduce and returns things(???) Are you a javascript programmer? If not, then it's ok if the code in an unfamiliar language looks cryptic. But if yes, then the code example (is it the one with the Promise.resolve as an accumulator?) is a very idiomatic way of writing sequential http requests before we had async functions and stuff.