Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course
The "Currying Example & Argument Order" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:
Brian gives the example of the filter function, curries it, explains the argument order, and imports the currying function from the Ramda library to be able to curry with more than two arguments.
Transcript from the "Currying Example & Argument Order" Lesson
[00:00:00]
>> When do you know when to curry and when not to curry?
>> So just as we kind of showed that we can flip arguments and we can take it all at once or we can take one parameter at a time with currying. You say I have a function and I have these options.
[00:00:17]
And I know they're always gonna end up in the same equivalent definition. So I'll choose currying when I want to remember an argument and pass that around. So I usually say I'm going to curry a function when there's an obvious kind of configuration to pass in, and it'll just remember that from then on but let's look at some more examples and you see why you might want to do that.
[00:00:44]
So let's leave these on the screen. Let's do filter. So we'll take an array of xs and some function f and we'll say well, there's a good example of bad argument order. [LAUGH] We'll take our function first, and then our array, and we'll say array.filter the function. Okay?
[00:01:08]
And we're going to go ahead and curry filter. What's gonna happen here is filter will run through the array and call our function on every value right? Hopefully everybody here knows filter as part of the pre reqs. I should have gone through the pre reqs you should know map and filter coming and reduce, that's the big one.
[00:01:31]
So if I said, well, I'm going to actually curry filter, which is odd. Now I have a function that goes through a list and say FiltersOdd or getOdds. So this will go through a list, let's say 1, 2, 3, 4, 5. And I could pass that into getOdds.
[00:01:57]
And what I should get out is just all the odd numbers if everything worked out all right, hey, works. And so, instead of having to write the entire filter upfront, I could define a whole new function in terms of just the curried functions this odd is just modulo curried to two, it's just remembering the two.
[00:02:16]
Filter is remembering that function argument is odd. And so now, I have a brand new function just by remembering arguments, which is pretty nice. And we go through the class, we'll start to see more uses of curry. One thing I wanted to call out here was the argument order.
[00:02:39]
So if I wrote xs first and then F here, xs being the plural of x. So that's what I call the array of xs. And then we have this function, you'll have a lot of these x y variables in functional programming. It's typically because we strive to write the most general functions as possible.
[00:03:00]
And so you don't have a name like filter will filter anything. What am I supposed to call it? So I'll just call it x. And if I have many of them, I'll call them xs. So that's just something that, people are, this is frustrating. I need to see names that I believe in.
[00:03:17]
If you generalize all the way to its logical end, you end up with x. And then when you're writing your program, you can redefine it as something that is domain specific, but in any case, here we are, if I give it Xs first, I wouldn't be able to curry this.
[00:03:35]
I don't have the array at the time of defining get odds. There's no array here. How would I? So I would have to say well I'll take an array. And then I'll call it with that. And that's not very useful. That's so what's happening here is I'm defining it with the arguments that I should remember upfront.
[00:03:57]
And then the data for which it operates on as the last argument. And that's kind of the general rule that you would want to follow when defining curried functions is the data it operates on ends up last. Let's write one more. And for this one I'm gonna actually bring in ramda's curry because import curry.
[00:04:21]
I can't use imports sorry we're not there yet. I'm sure hopefully like 2025 we'll have imports [LAUGH] but ramda cool equals require ramda. We have everything but imports at this moment in time I buried a time capsule with my statement. So we're gonna get rid of this curry and just use ramda's curry.
[00:04:47]
And the difference there is that I wrote a curry with just two arguments. Ramda's curry allow you to curry a function with as many arguments as you want. It does fancy tricks on F.length and it does all this very tricky performance optimization stuff. So we can do this without worry.
[00:05:08]
So all this should still work. I'm just bringing in ramda and you can look at the ramda docs but it's basically the same function as this. Just with as many as you want.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops