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:
Tweets by mpjme
http://www.quora.com/Mattias-Petter-Johansson
Original source
39 responses to “Currying – Part 6 of Functional Programming in JavaScript”
perfect tutorial, it's awesome. thx a lot
awesome
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?
I've used the curry function before but haven't realized that it could be more powerful when passed as an callback function to the filter
this is awesome
I am a simple Indian, I see curry, I press like. 😀
Yaaay. I am Indian and it took a Swede to teach me curry-ing. Hmm.
which will return a new function
So been watching this series lately and my question how do you get those fun words out
That was amazing! I am learning curried functions in SML, this video was such a help to get the concept. Thank you so much.
These videos are sooo good, thank you so much for sharing!
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 🙁
"post production leviosa" hahaha… 😀 😛
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
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');
I love the proof part. https://youtu.be/iZLP4qOwY8I?list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84&t=233
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 🙂
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.
But what is the difference between carrying and closures in this particular example?
stay curryous!
0:35 Great quote. Indeed.
"Does it sound confusing? Good! The feeling of confusion is your friend, it means you're learning."
Great work! Really enjoyed learning these new concepts so simply from you
Tks a lot. Great serie of videos !
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.
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
Awesome tutorials..a video from u on es6 generators would be great..
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.
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);
in atom a word "log" stands for console.log 🙂
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.
Once you get used to curried by default, the lack of it turns irritating. It is a very useful thing.
Isn't it as twice as shorter and much easier to grasp, as for this particular task? We do not need Lodash as well.
let lightingDragons = dragons.filter(x => x.element == "lightning" && x);
May be example itself is not the best one to make Currying shine…..
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.
so one of my boys told me to watch this videos awhile back, I was like nah. But now I'm like dayuuuum. Good job man
These are excellent solutions when you in advance know how many arguments will be passed into the function. How do you curry when you are NOT aware of how many arguments will be passed in?
The constant feeling of confusion means I'm dumb
MIND. BLOWN!!!!
This is fantastic. I can't find examples that good anywhere else, thank you!.
The currying function doesnt run in my google chrome console, doesnt it support ES6?
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…