3 ms·
As a side note, using .push() isn't something I would consider being functional because immutability is a core concept of functional programming. Too bad, Java
by cygned 9y ago
As a side note, using .push() isn't something I would consider being functional because immutability is a core concept of functional programming.
Too bad, JavaScript makes it hard to do it that way, in that concrete example using Object.assign() and .concat().
- jlg23 9y ago> immutability is a core concept of functional programming. That depends very much on your definition of FP (or how "pure" the FP is, depending on your definition of "purity").
- cygned 9y agoFair point. All my touch points (Lisp, JavaScript, even a bit Haskell and several books) all proposed immutability as being a fundamental principle. Would you mind expanding a bit on that point in case I totally misunderstood something?
- jlg23 9y agoYou did not "totally misunderstand something": Immutability is a virtue in FP, you should write in this style. But it is only a "fundamental principle" and "core concept" in highly pure languages - and even Haskell allows mutable variables[1]. Sometimes you just need them, e.g. when handling potentially very large vectors in finite memory. CL's (sort ...) for example mutates the input sequence (or "destructively sorts" it [2]). [1] https://wiki.haskell.org/Mutable_variable https://wiki.haskell.org/Mutable_variable [2] http://www.lispworks.com/documentation/HyperSpec/Body/f_sort_.htm http://www.lispworks.com/documentation/HyperSpec/Body/f_sort...
- qrohlf 9y agoagreed, the Array.prototype.reduce could be accomplished in a far more readable way using a simple forEach. If the function was doing a `concat` instead of a `push`, it would make a lot more sense to me as a functional example, because then the author would actually be taking advantage of the way the accumulator result from the previous call is passed into the next call.