News for hackers. Stuff that matters.
Re:Functional Programming in 5 Minutes by lelandbatey on Sunday May 19, 2013 @06:18PM (#5734635)
OOOOOOh, ok I see how this is working a bit, I think. I am very inexperienced with Javascript, and I think I only just "got" what was being done here, so I'll try to explain my thought processes as a newbie:

So, in Javascript, variables can be functions. Which means you can pass in a function as a variable to another function. And the part that says h(h(y)), basically says that "h" has to be a function. Which means you pass in a function, and then it get's applied to itself in the way specified within that function, "g".

Another odd part is the function you pass in:

    g(function(x) {
        return x * x;
    })(3);
because you are passing in a function, but I'd assumed that if you can only pass in one variable, and that variable has to be a function, then it seemed like you wouldn't be able to pass in an initial value for the function you want to apply. But I guess in Javascript you can pass in a value for an anonymous function defined inside a function call by using this syntax:

    g(function(x) {
        whatever it is that happens in this function;
    })(some_value_to_be_passed_into_the_previously_defined_anonymous_function);
Re:Functional Programming in 5 Minutes by Twisol on Monday May 20, 2013 @12:00AM (#5735796)
`g(f)` returns a function, which is then immediately called with another value. You're passing `3` to the result of `g`, not to the function you passed to `g`.
Re:Functional Programming in 5 Minutes by lelandbatey on Monday May 20, 2013 @01:40AM (#5736094)
Ah, I see, that makes sense. Thanks for the explanation.
Re:Functional Programming in 5 Minutes by minikomi on Sunday May 19, 2013 @06:35PM (#5734691)
Just to help in case it isn't obvious - g also returns a function - which could be assigned to a variable. In this case it's called straight away but you could do it like

    var powerOfFour = g(function(x) {return x*x;});
    powerOfFour(3) === 81; //true