This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.
Transcript from the "Currying Exercise Solutions 1 & 2" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Joe Nelson: So we saw how to make a double function.
>> Joe Nelson: So here's the way that I solved one. So words, we have our split so what does the challenge say? It says make a function called Words which returns a list of words in a string. Use only the split function and currying.
[00:00:22] Now I guess what slightly confusing is the currying is not something you have to go out and do. The currying is baked into split and baked into a lot of these functions. So that if you know a function has been curried, then you can say to yourself okay, the docs say that this function takes two, three, four, however many arguments.
[00:00:43] But I'm free to give it one argument, two, whatever at a time. You can give it fewer arguments and it'll be behave and it'll be waiting for its next ones. So split takes two arguments, what do I look for in the string to split around and then give me the string and I'll do my thing.
[00:01:00] So I said okay, words, I called split. All I told it was here's what you split around and because it's curried, it says okay, I can work with that, I'm ready. Give me a string, I'm a new function, I'm just waiting for a string. I know what to split around now.
[00:01:14] So I say words equals split with one argument.
>> Joe Nelson: So the beauty, I guess, the reason we're including these exercises is to say you don't, I named it here, I called it words. But sometimes you don't have to make new functions to do things. You don't have to name them.
[00:01:33] You just use the ones you have but they're written in a way that they can kind of be partially filled in. So, that's the first Challenge 1.
>> Speaker 2: So, now the split, that's not the JavaScript split, right? That's actually in the ramda library?
>> Joe Nelson: Yeah, it is.
>> Speaker 2: Okay.
[00:01:49]
>> Joe Nelson: It is. You could make a curried version of the JavaScript split if you wanted to, but they've already done it.
>> Speaker 2: Well, and that's part of what I think's a little confusing is knowing okay, this is actually something predefined for us in underscore dot, although it's not underscore, it's the ramda which is currently mapped to the underscore variable dot.
[00:02:11] And then on that ramda, we're calling the split function, which has already been curried for us.
>> Joe Nelson: Correct.
>> Speaker 2: Okay.
>> Joe Nelson: Yeah, yeah,so I guess some clues about our vague intentions here. When you see underscores in the code, like underscore.something, okay, that means it's been supplied to. And it's not from the underscore library, it's just that we wired things up.
[00:02:32] Let's see if it's in our set up code at the top. If it's not then it's in our HTML or maybe it's in our, wait, yep, right up at the top [CROSSTALK] Line one says, so when you include ramda. I think ramda works with like AMD modules and stuff but I think it also exposes this global called ramda.
[00:02:49] So I just assigned ramda to underscore because I wouldn't want to type ramda.split, it's kind of long. Although it might have been less confusing, so, yeah.
>> Speaker 2: On this first one, it would've.
>> Joe Nelson: [LAUGH] On the first one? So yeah, things that have underscore before them are from ramda.
[00:03:09] Okay, so we did the split thing, that was number one. Challenge number two, unless there a questions? Okay, create a function to triple every number in a list using only multiply and map, cool. Well, we saw one way that multiply was used with currying. We made a double function by passing it two so that it's still waiting for another argument but it's gonna multiply it by two.
[00:03:34] Well, we wanna triple the things in a list so the first thing that comes to my mind is I'm gonna modify the double thing and make a triple thing where I just pass it three. Is there a question?
>> Speaker 3: Yeah, Raymond W had a question about whether we'll be talking about what the best order for supplying arguments to functions is to support efficient currying.
[00:03:55]
>> Speaker 4: That's a good thing to talk about.
>> Joe Nelson: Generally, I mean, go ahead.
>> Speaker 3: No, you go first.
>> Joe Nelson: Okay, generally, you let the argument be the last thing. Because if you could fill in some little values like here we had, let me think, like split. I could have said now split takes a string too, like map.
[00:04:14] No, that's a bad one too, like reduce. [LAUGH] I don't know. If something can take a function, let it take that function last and let the little scalars be first. Because then, you could fill in the scalars, you still have a function.
>> Speaker 3: No, that's okay.
>> Joe Nelson: No, that [LAUGH].
[00:04:32]
>> Speaker 3: Basically, the data you're operating on, like let's say you're getting an API call of information. That's the thing you want to go last so you can partially apply everything up to this point of the thing that you don't have. You have other functions available so you can combine them.
[00:04:46] And you have arguments like split space, I have space, I can get that to split, but I don't have the string yet. So think about the thing that's gonna go into your function as the thing you don't have, the data flowing through your app. That's the way I look at it.
[00:05:00]
>> Joe Nelson: Right, right, cuz the string, right. So in our words function, it was nice that we could be like, I want to split on space. Space is a little thing, I have it in hand. But a string, I'm not likely to know what that string is. I'm splitting some kind of data, it's given to me, it's something I don't know.
[00:05:16] So let the things you don't know be last so you can fill in the things that you do. And then the remaining function is waiting for the thing you don't know about.
>> Speaker 2: So the currying, you're supplying from the first argument forward?
>> Joe Nelson: Right.
>> Speaker 2: So if I give it two arguments it will fill in those first two and then the third one is unfound?
[00:05:34]
>> Joe Nelson: Yeah, it keeps being empty at the end, waiting at the end.
>> Speaker 3: So, I mean one thing that's really cool about that is if I want to make a function that replaces dashes for slashes. I can give a replace my dash comma slash and then the last argument would be string, because I don't have my string yet.
[00:05:54] So I can just make a new function out of some arguments. But if I had to have my string, now I can't make that function. I actually have to go to a string and call dot on it and that's the difference between your dot syntax and your functional functions here that you can combine is if you don't have your data yet, you can still build an app.
[00:06:12] But with the dot syntax you have to have your data so you can call dot on it. It's still functional that way too, by the way, I should mention that. It's just that it's a difference between dots syntax and not dot syntax.
>> Joe Nelson: So, thinking about our triple listing again.
[00:06:29] Well, let's think about it. It's gonna act on a list, that's one thing. But there's gonna be tripling. So, how would we triple? We'd say multiply three. It's a function still waiting for its other argument, you hit it with three. Now we wanna do it to a list.
[00:06:45] And we take a list and make a new list element by element with map. So we wanna call map and what is it that we wanna do to each element? Well that's easy, we want to triple it and we already have a triple function. So let's see if it actually passes.
[00:07:14]
>> Joe Nelson: Wonder if there's some stuff from earlier?
>> Joe Nelson: Unexpected semicolon, okay, let's see.
>> [INAUDIBLE]
>> Joe Nelson: Yeah it's not, but maybe the actual real console does.
>> Joe Nelson: Runner:62, that sounds, boy. Under max, I was playing around with some other stuff, let's get rid of that.
>> Joe Nelson: I had begun typing that bit.
[00:07:59]
>> Joe Nelson: There we go, okay, so that made this one pass. So why did this happen and to recap. We wanted a thing that would triple everything in a list. The way that you do things to things in a list and give you a new list is with map.
[00:08:10] What would you like to do to each thing in that list? We would like to triple it. So it's two couriers just plowing together.
>> Joe Nelson: Yeah, right, so if there's some really complicated, gnarly stuff. As soon as you edit this jsbin, you'll get your own number and you can just paste that in the chat and we can look at it together.
[00:08:47]
>> Speaker 3: Yeah, do you want to look at this one together?
>> Joe Nelson: Which one?
>> Speaker 3: We're looking at Valentine's in the chat, he just pasted in his jsbin. That'd be good to see what challenges [INAUDIBLE]. I think it's still you [CROSSTALK]. Yeah.
>> Joe Nelson: Wait, it's over, okay, I'll read it.
[00:09:30]
>> Speaker 2: Yeah, and his failing example one.
>> Speaker 3: Yeah, it's the way the messages are kind of set up to show.
>> Joe Nelson: [INAUDIBLE] So that's jsbin-
>> Speaker 3: D-E-Y-I-Q-I-J-U forward slash mark.
>> Speaker 3: I think you're on output, not content.
>> Joe Nelson: Thanks.
>> Speaker 3: So looks like words were split with I.
[00:10:12] It's just a confusing, the error syntax there, where the errors [CROSSTALK]
>> Joe Nelson: I see, I see, I see. So yeah, the error is confusing. The way I'm thinking about it though is we should think about what split does. So when you look at the log, you see how it's like the Mississippi is like m, ss, ss, pp, blank?
[00:10:36] Split, the first thing you give it is what to split around. So if I ask you to split around i, if I had this thing, I'm calling it words. For every word in the list, I want it to break that word apart. If I split on i and I put Mississippi in, I wouldn't get an array of Mississippi, it should be splitting around spaces, or is it-
[00:10:59]
>> Speaker 3: Yeah, [INAUDIBLE]
>> Joe Nelson: Well, I'm wondering whether there's some other reason this failing or if this is just the whole thing?
>> Speaker 2: Well, 36 should have a space not an I in the square.
>> Speaker 3: Yeah.
>> Joe Nelson: I didn't know if it was a more subtle. So when you split around the thing you want to split, yeah, there we go.
[00:11:15]
>> Speaker 3: Well, the trouble with Valentine was struggling on challenge two because challenge one was failing. But it thought, because the error message showed up about one, two, three,it was, I don't know. I think it was more of a confusion around the error message than the actual example.
>> Joe Nelson: That's what I was wondering, whether it was, yeah.
[00:11:34] So note to self, better test suite.
>> Speaker 3: [LAUGH] yeah.
>> Joe Nelson: [LAUGH] Next time.
>> Speaker 3: Mine's gonna be horrible. You're just gonna be like, what's happening?
>> Joe Nelson: Yeah. [LAUGH]