Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course

The "Currying Solutions" 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 live codes the solution to the currying exercises.


Transcript from the "Currying Solutions" Lesson

>> How's that? Everybody feel? Okay? So really angry with me because the hardest thing is to write these examples and have them like actually make sense. And try to put you in a situation where it's like realistic. So, all right, we can go through and answer these one by one.

I should also mention for those of you in the classroom online, there's these other code pens available. If you want to jump ahead we won't be talking about common ads or mono ads today or anything like that. So if you want to jump in my coat pens and look you can.

So right words, so we're going to do is say, all right, well split takes one argument at a time, string last. So here's a function that takes a string. But if I just call split like that, with its argument, it's now a function that takes a string, right?

Okay, one way to look at it is if I take a string and then put it here. You can kind of cancel out the arguments, like cross them out [LAUGH]. It's kind of like a little algebra trick, there you go. Cool, and we're still passing down here, here we have map, an just like we crossed out the last one, we can get rid of these x's here and these x's.

And map function to where two arguments takes its data last, we can just say map words and still passes down there exercise one A. So it's nice, we got rid of all that kind of unnecessary names and ceremony and just got right to the definition. Sentences is just mapping words [LAUGH] after the array and grabbing all the words from it.

Exercise two this one is along the same lines it's got a lot of function noise in here. I didn't use arrow functions to define these because it just looks so much fun to watch all the noise fall away. So, one of the we are updating mostly out of quick guide to like, eat snacks, and I was like, it's not as cool anymore, because arrow functions are so nice.

So, we have a function that takes x's. Let's start off by saying okay, we're going to take these x's and return a new function. We can just cross these out here, right? So let's go ahead and say, filter. Keep it passing at least. Filter is a function, it took x's, we'll just get rid of x's there and here and now it's a function that takes x is still.

Then it's blowing up. There we go. But we can take it one step further. Test is a function that takes in argument and we can keep that grade and just remove all these. All we need is that. So there we go. I mean we're just saying we're gonna test for any cues, we're gonna filter an array that does that.

All right, let's jump on to the next one. Keep highest is just this little helper function just takes the max. And what we're going to do is rewrite this in its simplest form and something I didn't touch on yet. I'm glad we're hitting this is that. We didn't talk about callable functions and let me just hop over here and reason this fun new, fun new program to do our examples because it's so clean look at this.

All right, so if I have a function called add again, right and takes its x and y and it just adds them. Add if I just console that log add it is a function right. This is never helpful.
>> [LAUGH]
>> Just so you all know [LAUGH]. If you're ever gonna build that it is never helpful.

So all right, so add is a function and if I call it with arguments, it's now a value. But if I don't call it, it's just a happy little function waiting to be called. I should have been a little bit clearer with that upfront. But hopefully you all had the prerequisite of just understanding JavaScript is the core idea of JavaScript.

But since we know that we can just pass functions around like that. We can actually say, well, a function that takes an accumulator and an x will key pious is a function that takes that same accumulator and an x. Why am I putting in a function and just throw it in the same argument through, I could just remove this entire function.

Do that right off the bet. Okay, so there's one step. Do you all see how that works? Since I can put them side by side, but reduce will pass its accumulator and value and to keep highest. Keep highest takes those two arguments. So I don't need to actually pass in an accumulator and an x.

And simply call a function that takes an accumulator and x. I can just get rid of all that noise by passing in the first class function. Now, here are a lot of you squirming in your chairs and JavaScript has all these bizarre functions that take variadic like arguments and sometimes this gets you into trouble.

So sometimes this is the right answer. But I just wanted you all to have a clear understanding of this being because reduce, for instance, takes passes an extra argument. This one just throws it away. But that's just something to know. All right, last thing is, again, we can cross out the x's.

So let's go ahead and do that. And we took that giant ceremonious hard to understand reduce everybody, like all reduces that somebody called it fancy. They're like, hey, can you remove all that fancy JavaScript you've been writing, I was like what reduce? But it's, yeah, when you when you're able to define it in these very clean terms, it almost reads like English.

And I think that's very nice. Last couple here before we jump into composition. So define slice and we want to make it carried. So slice is gonna to take one argument at a time. So it'll take on our xs. Well, taken our start in our end and our xs last, that argument order.

So our data last and we'll just turn around and call a slice. With the start and end. You don't typically have to do this, because it's probably built into a library, if you're doing this kind of programming. All the kind of standard library functions will show up curried for you arguments last.

There we go. Should be passing bonus one, terrific. And then bonus two says take. It's gonna take n elements from an array. So if I say take two, it'll give me the first two. So I can just slice, slice, the, wait a second. Getting confused here. We're going to take n, right.

We're going to slice n does that work? Doesn't work. Someone helped me out. I haven't done this in like three years. [LAUGH].
>> Provide this start as zero.
>> That's a great idea. We'll start with zero. Thank you. Yeah, starting at zero and two, that makes so much more sense.

So these are kind of like a few examples where currying really pays off, right? We've seen all this code, just kind of dissolve into the background that was just noise it was we lost all the signal in favor of this ceremonious noise everywhere. And, I don't want you to think that you should always curry things and the smaller code is better code or whatever.

I'm just saying, if you define sometimes this makes the most sense in this situation. To define a very clean function for reuse rather than having to define a full function and all its arguments and name those arguments and so on.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now