from Hacker News

Functional JavaScript, Part 4: Function Currying

by lrichardson on 4/29/14, 4:01 PM with 23 comments

  • by chriswarbo on 4/29/14, 5:13 PM

    Interesting, especially aside about partial application ("holes") which I've not seen done in JS before.

    I've been using my own JS currying implementation in a few projects[1], which pretty much matches the implementation in the article: count the function's parameter number and build up argument lists in closures until we have enough to call the function, then pass them all in.

    However, when I was implementing the same thing in PHP[2] I discovered a nice alteration we can make: instead of passing all arguments to the curried function, we should only pass the minimum number. If we have any left over, we should pass them to the return value. This lets us chain even more things together[3]. For example:

        // "c" is our currying function
        var id     = c(function(x)       { return x;         });
        var triple = c(function(x, y, z) { return x + y + z; });
    
        // These work with both currying functions
        id(triple)('Hell', 'o Wor', 'ld');
        id(triple)('Hell')('o Wor', 'ld');
        id(triple)('Hell', 'o Wor')('ld');
        id(triple)('Hell')('o Wor')('ld');
    
        // These only work with the altered version
        id(triple, 'Hell', 'o Wor', 'ld');
        id(triple, 'Hell')('o Wor', 'ld');
        id(triple, 'Hell', 'o Wor')('ld');
        id(triple, 'Hell')('o Wor')('ld');
    
    [1] http://chriswarbo.net/index.php?page=news&type=view&id=curry...

    [2] http://chriswarbo.net/index.php?page=news&type=view&id=admin...

    [3] http://chriswarbo.net/index.php?page=news&type=view&id=admin...

  • by platz on 4/29/14, 8:53 PM

    Seems to me that using the functional style it's really important to document the types (done in the article with the //+ lines).

    Otherwise it will be hard to make the types line up.

    Also there are functions in javascript which do different things depending on what the type of the argument is (or do different things depending on how many arguments are passed in; those kinds of functions always have irked me), so it would be hard to document exactly what the "type" of those functions are.

  • by ceedan on 4/29/14, 7:07 PM

    "In most web applications the "bottle-neck" is going to be DOM interaction. It is very unlikely that you will notice a performance hit at all."

    uhhh, 2 wrongs don't make it right?

    "It is very unlikely that you will notice a performance hit at all."

    Until you do..

    --

    I could never justify using this stuff at work. It's inefficient, unfamiliar to most JS developers and even if it did pass a peer review, nobody else would actually want to maintain it.

  • by taylodl on 4/30/14, 2:00 AM

    Another motivation for currying in JS is to memoize the curried function which I describe in my post http://taylodl.wordpress.com/2013/11/05/functional-javascrip...

    I never provided a currying function, thinking there were so many implementations easily available - but I've never seen one as good as what's presented here!

  • by ripter on 4/29/14, 10:02 PM

    Awesome job! I was trying to understand curry by learning Haskell but you summed it up perfectly.

    What is the difference between curry and partial? Is a partial just a curry that hasn't filled in all the arguments yet?

  • by rpwverheij on 4/29/14, 4:23 PM

    being a javascript/typescript developer with little knowledge about functional programming, this article renewed my interest in the matter. I would be interested to see some performance tests though, to get a feeling just how much 'a little performance cost' costs
  • by batmansbelt on 4/29/14, 4:21 PM

    Seems like a cool use of the technology.