Currying – Part 6 of Functional Programming in JavaScript




A short video explaining the concept of curring, using JavaScript. This is part of a series, where are learning functional programming using JavaScript.
Currying is when a function, instead of taking all arguments at one time, takes the first one and returns a new function that takes the second one and returns a new function which takes the third one, and so forth, until all arguments have been fulfilled.

Curry function in lodash:
https://lodash.com/docs#curry

Playlist of full series

You want to follow me on Twitter and Quora:

http://www.quora.com/Mattias-Petter-Johansson

Original source


39 responses to “Currying – Part 6 of Functional Programming in JavaScript”

  1. MPJ, you're the man. I always check out your videos when I need to brush up on a topic. With that being said, how come you used arrow functions but not template literals in this video?

  2. Hey MPJ … How come the Multiline String is working without backquote. Also, Why haven't you enclosed the returned string within curly braces as per arrow function rule ? I am confused 🙁

  3. var dragon = function(name){
    return function(size){
    return function(element){
    return name +' is a '+size+' dragon that breathes '+element+' !';
    }
    }
    }

    console.log(dragon('Drago')('huge')('fire'));
    Here is the exmaple in plain JS

  4. Is the code below the ES5 equivalent to the currying example shown @2:29?
    var dragon = function (name) {
    return function(size) {
    return function(element) {
    return name + ' is a ' +
    size + ' dragon that breathes ' +
    element + '!';
    }
    }
    }
    dragon('fluffykins')('tiny')('lighting');

  5. 1. Thanks 4 video
    2. i think that readability is in first priorety in any kind of concept.
    as fa as i see the curring concept is not so readable and Omit stuf, that if other developer come to see the code it will take a lot of time to understand it. it think that in this case more code is not bad and more readability is perfect 🙂

  6. good video, could have been better, I watched it twice and still don't see the point of currying, except as a shortcut when using filter (or others), even though their is less code it starts to look more confusing and less readable.

  7. I wished this entire series was more about functional programming paterns, this can be understood just by reading MDN, paterns, are an other story. Still, never used _curry, even if I used underscore or lodash everyday before es6, so thank you, good teacher by the way. Nor sure if curry is worth it, it's not super readable, but always good to know.

  8. So glad I stumbled across your vids. I am currently falling in love with javascript because functional programming <3. fun examples, fun explanation, with style. Entertainment learning += subscriber

  9. I'm not sure that currying is a good concept, maybe would make the code less maintainable, even tho it might be more concise. just feel that the cons outweigh the pros.

    just my $.02.

  10. in ES6 without lodash or any deps:

    const dragons = [
    { name: 'fluffykins', element: 'lighting'},
    { name: 'noomi', element: 'lighting'},
    { name: 'karo', element: 'fire'},
    { name: 'doomer', element: 'timewrap'},
    ];

    const hasElement = (element) => (object) => {
    return object.element === element;
    };

    const lightingDragons = dragons.filter(hasElement('lighting'));

    console.log(lightingDragons);

  11. Why do you prefer to put curried parameters on their own line instead of Haskell style one line? I find the one line more readable and clearer. P.S. I don't know much JavaScript.

  12. Nice video. Thanks for that.
    I've never known that there is a `partial` and `currying` functions in lodash.
    I've been using `bind` all the time for that. As you mentioned in the comments `currying` and `partial` are different, and it would have been nice if you'd mentioned it in the video and elaborate on it. Because when I used `bind` I though I was doing currying, and I believe lots of people confuse them too.

  13. A great video as always. Thanks for all you do. 🙂 This works for a fixed number of arguments – what if I want to have an indefinite number of function calls though? For example, adding or multiplying a bunch of numbers. Maybe a bad example as it's probably easier to take all the arguments at once, convert them to an array and then reduce, but I'm interested in principle…

Leave a Reply