Transcript from the "Function Challenge 2" Lesson
>> Douglas Crockford: Here’s a next one. Write a function curry that takes a binary function and an argument, and returns a function that can take a second argument. Here we'll pass to the curry function the add function that we wrote this morning and 3. It will return a function that will add 3 to things.
[00:00:21] And similarly we could pass to curry the multiply function and 5. And it will return a function that will multiply things by 5.
>> Speaker 2: We did curry yesterday.
>> Speaker 3: [INAUDIBLE] It all comes full circle.
>> Douglas Crockford: It's not uncommon when I introduce this problem, that some people perk up.
[00:00:48] They go, Curry this is finally starting to get interesting.
>> Speaker 2: In the previous solution we had a var add f.
>> Douglas Crockford: Mm-hm.
>> Speaker 2: And earlier on we had a function add f.
>> Douglas Crockford: Yep.
>> Speaker 2: And JS Lint complains it's a redefinition.
>> Douglas Crockford: That's right.
>> Speaker 2: And is?
>> Douglas Crockford: JS Lint is right.
>> Speaker 2: Okay.
>> Douglas Crockford: Okay, so here's a curry function. Curry takes a binary function and a first argument and returns a function that takes a second argument. And returns the result of calling the binary function with the first and second arguments. So who got curry, very good. If you didn't get curry, get it now because you'll going to need it later.
[00:01:36] Now another way you could have written curry, is you could have used the lift function that we wrote earlier. Did anyone use lift f? That would be extra credit, if you've done that. So, this process of taking a function with multiple arguments and turning it into multiple functions that take a single argument is called currying.
[00:01:56] And it's named after Haskell Curry who is a mathematician who did a lot of work with Church's lambda calculus. And by normalizing all functions to take only one argument it made a lot of operations easier to think about. Some people think it should be called Schonfinkelisation because there is a fellow named Schon Finkel who is doing this stuff earlier but we're going to call it currying.
[00:02:39] You have to do that and it's inexcusably awful, and it's because the arguments array isn't really an array and so it doesn't work right, and so you have to do all these tricks in order to get things to happen. I'm not going to explain what this does because I just don't want to waste time on it, it's awful.
[00:02:56] And so because of this awfulness. All of the functions that we write today will only take a fixed number of arguments. So we'll have unary functions will take one argument, and binary functions will take two arguments even though lots of the applications we're going to do would really like it to be variable.
[00:03:13] I don't want you have to waste your time on this because you can see there, there's a whole lot of stuff going on that has nothing to do with curry. Now in ES6, which is starting to find its way into implementations. There is some new syntax with the ellipsis operator the dot dot dot operator.
[00:03:33] And if you put the ellipsis in a parameter list, what that says is, take all of the rest of the arguments all the remaining arguments put them into an array and bind that array to that parameter, and that is so much cleaner. And then on the call side in an argument list, if you go dot dot dot and there is an array there, it says take that array and spread it out.
[00:03:58] So each element of the array will be a separate argument. And so by doing that, we've got this version of the curry function which looks exactly like the first one except it's got the annotations in it which says, this is where you can have a multiple number of things being passed.
[00:04:14] Both of these do exactly the same thing, which one would you rather be reading? I'm guessing it be the second one. So when ES6 becomes everywhere, then I'll change this course so that we'll be writing functions that look like that. Yeah.
>> Speaker 4: That's kind of questions I had for you yesterday, but it's clear now.
[00:04:36] So like, when you were seeing the advent from ES32, like ES5, that take like a long time for browsers to adapt? Or do you see us moving a little faster now or is it just going to take quite a while to [INAUDIBLE].
>> Douglas Crockford: It's faster that the big hangup with ES5 was the IE problem.
[00:04:55] And IE is not nearly the problem that it was before. And all of the other browsers are out updating. So the problem isn't propagation and adoption. The problem is now just implementation. So I'm hoping that we'll get all of the stuff out much faster. So anyway, Ellipsis will be my second most favorite feature ES6, when and if it ever gets finished so that will be great but in the mean time we're only gonna be concerned with functions taking a fixed number of arguments, Okay?
[00:05:31] So we're ready for the next one? This next problem is going to be a little bit different because you are not going to write any new functions. Instead you're going to be using functions that we have already written. So without writing any new functions show three ways to create the inc function.
[00:05:47] The inc function adds one to a number and returns it. So if you pass five to inc it will return six. And so you're going to call a function, which will create the inc function. And you're going to show three different ways to do that using functions you've already written.
>> Douglas Crockford: So here are three ways to do inc. First one is add f of 1, who got that one? Okay, the next is lift f of add and 1, who got that? And curry of add and 1 who got that? And who got all three? Very good, so this illustrates the first rule of functional programming which is, let the functions do the work.
[00:06:33] If you've already written a function that does what you need, you don't need to write another one.