Hacker News new | ask | show | jobs
by mkirklions 2958 days ago
I spent about 30 minutes reading, but I didnt understand why this has 140 points and is the top thread.

Can anyone explain?

1 comments

Functional programming (FP) is an important paradigm with many practical benefits, such as preventing bugs, improving parallelization, etc.

JavaScript is a language in which one can apply the FP paradigm, with some (considerable) effort. This book explains both the underlying FP paradigm and how to apply it in JS.

Other languages (e.g. Haskell, Scala, F#) are designed for the FP paradigm and make it much easier to apply. But the paradigm itself is the same for all of them, as it arises from fundamental mathematical laws.

> with some (considerable) effort

disclaimer: I haven't read the book and am not sure if this is mentioned anywhere.

What helped me when thinking about functional design in javascript was realizing that all js functions actually only have one parameter, an array of arguments used by the caller:

    function add(x, y) {
        return x + y;
    }
is effectively syntactic sugar for

    function add() {
        const x = arguments[0];
        const y = arguments[1];
        return x + y;
    }
`add(1,2,3,4)` ignores 3 and 4 instead of being an error. While seemingly obvious that these two functions would have different definitions: `add1(1)(2)` and `add2(1,2)`, thinking about it in types helped me process it when thinking out how they are actually written:

    add1 :: [Number] -> [Number] -> Number
    add2 :: [Number, Number] -> Number
Yes. This is the essence of currying.
Thank you!