|
|
|
|
|
by taeric
4819 days ago
|
|
And, see, I would actually invert further. The declaration should be: elements = [1,2,3,4];
doubledElements = doubleElements(elements)
Basically, if you see the words map, fold, reduce in your code, you are probably not as easy to understand as you'd like to think.Of course, in the cooking methaphor, I'm ok with mutating elements and just doing: elements = [1,2,3,4];
doubleElements(elements);
This clearly has issues if multiple "cooks" are working with elements. But is ridiculously easy to intuit regardless. (Precisely because in real life so many things are changed by imperative commands.) |
|
But then you lose pureness, right? The whole point of using high-order functions is allowing you to be as declarative as mathematics, so you can just operate functions together.
Consider that in the first example, I only need to write the implementation for doubling a number n, while the `doubleElements` implementation is too specific, would throw the other half of the code back into imperative land.