The full video and many others like it are all available as part of our Frontend Masters subscription.

Brian Lonsdorf

Brian Lonsdorf has taught several workshops and training courses on functional programming with javascript. He'a a regular speaker at conferences and helps organize/host FP events around the Bay Area.

Brian Lonsdorf

Hardcore Functional JS

Joe walks through the solution for the currying exercise. He starts with challenges 1 & 2. Joe then gives the audience more time to finish the 3rd challenge in this exercise.
- Problem: http://jsbin.com/romun
- Solution: http://jsbin.com/mojusuyane

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.

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.

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.

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.

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.

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.

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.

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.

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 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.

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: On the first one? So yeah, things that have underscore before them are from ramda.

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 going to multiply it by two.

Well, we want to triple the things in a list so the first thing that comes to my mind is I'm going to 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.

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.

No, that's a bad one too, like reduce. 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.

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.

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 going to 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.

Joe Nelson: Right, right, because 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.

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?

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.

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.

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.

Well, let's think about it. It's going to act on a list, that's one thing. But there's going to 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 want to do it to a list.

And we take a list and make a new list element by element with map. So we want to call map and what is it that we want to 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.

Joe Nelson: Wonder if there's some stuff from earlier?
Joe Nelson: Unexpected semicolon, okay, let's see.
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.

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.

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.

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. I think it's still you. Yeah.
Joe Nelson: Wait, it's over, okay, I'll read it.
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: 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. It's just a confusing, the error syntax there, where the errors
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? 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-

Speaker 3: Yeah,
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.

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.

So note to self, better test suite.
Speaker 3: Yeah.
Joe Nelson: Next time.
Speaker 3: Mine's going to be horrible. You're just going to be like, what's happening?
Joe Nelson: Yeah.