Check out a free preview of the full The Good Parts of JavaScript and the Web course

The "Function Challenge 10" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:

In this challenge, you will write the addg(), liftg(), arrayg(), and continuize() functions.


Transcript from the "Function Challenge 10" Lesson

>> [MUSIC]

>> Douglas Crockford: Okay, let's look at addg. Addg takes a first argument, it creates a more function. If the first argument is not undefined it returns the more function. The more function takes the next argument If the next argument is undefined it returns first otherwise it adds next to first and returns itself.

By returning itself allows for the next thing to happen and I call this return recursion. Recursion is when you have a function that calls itself recursion is when you have a function that returns itself. So anybody do anything else? Take a different approach to solving this? There are lots of possible solutions.

>> Speaker 2: You're doing the same thing where you compute ahead the next value. That you, you did it in another part another one yesterday.
>> Douglas Crockford: So if you didn't get this one, please write it down cuz we're gonna need it for the next one. Anybody else?
>> Speaker 3: Well, it is different and I don't know how to read it and say that.

>> Speaker 3: The effects is undefined, return undefined. I don't know, I don't see anything, never mind, keep going.
>> Douglas Crockford: All right.
>> Douglas Crockford: So I'll give you a minute to capture this. So while that's happening can anyone guess the name of the next function that we should write
>> Speaker 3: Lift g, lift g

>> Douglas Crockford: Yeah very very good, lift g, of course. What else would you want to write next? Write a function liftg. That will take a binary function and apply it to many invocations. So it's similar to what we've done before. If you pass the add function to liftg it gives you the Aggi function.

We can do the same thing with multiply you make the multifunction.
>> Speaker 2: If you passed add into liftg, what would the value of the first operation be?
>> Speaker 2: What would you get back from the first time you call?
>> Douglas Crockford: Undefined.
>> Speaker 2: It would be undefined.
>> Douglas Crockford: Well if you were to call the resulting-

>> Speaker 2: No, instead of passing in mul, you pass in add.
>> Douglas Crockford: Right, although so you get an undefined, you get 3, and then you’ll get 7. And then you'll get 7 and 0 against the form.
>> Speaker 2: You got 3, 0.
>> Douglas Crockford: Yeah.
>> Speaker 2: But with mul you’re multiplying 3 times 1.

>> Douglas Crockford: All right so here is liftg, liftg returns a function, that function gets the first argument if first is undefined it returns first. Otherwise it returns the more function. The more function gets the next argument if next is undefined it returns first. Otherwise it sets first to the result of the binary function with first and next And returns itself.

So who got something that works outstanding very very good. Any questions about this one, anybody do a different approach that they think is worth mentioning.
>> Speaker 4: I do a great work but I tried First option number.
>> Douglas Crockford: Right so if you didn't get this one get it down.

You might want to refer to it in the next one.
>> Speaker 3: This is certainly good yet you function inside Inside the new function and generalize the.
>> Speaker 3: The ig function.
>> Speaker 2: So the first time if you call this with nothing then you would, you pass in undefined
>> Speaker 2: You just get back itself.

>> Douglas Crockford: Right we return undefined so.
>> Speaker 2: The next time when you, so if you pass in a number.
>> Douglas Crockford: That number is now first and then the second time if we get undefined we return first.
>> Speaker 2: The number is now First I see. Yeah great.
>> Douglas Crockford: Write a function array G that will build an array for many invocations So if we call array Gand pass it nothing It returns an empty array.

If we pass it three and then nothing it returns array containing three. If we pass it through three and then four and then five, and nothing returns three, four and five. Okay so here is arrayg, arrayg takes a first argument, it creates an array. It makes a more function and it returns the result of calling the more function with the first argument.

The more function gets the next argument if next is undefined it returns the Array. Otherwise it pushes the next argument onto the Array, and returns itself. So who got something that works? Very good, very good. Did anybody try using liftg? Anyone? That would have an extra credit all right.

So let's do another one. Okay we've I've got got this is a good one for you. Make a function continuize that takes a unitary function and returns a function that takes a callback and an argument. There's a style of programming called continuation passing style. In which functions, instead of returning values, instead are given an additional argument which is the function to which to deliver that value and in continuation passing style computation is always going forward, functions tend not to return anything.

They're always going forward and it's amazing. And when JavaScript gets tail recursion with ES6, then we'll be able to do that in JavaScript and that will be great. So this function is intended to help us to migrate toward that style of programming. So what it'll do is Allow us to take any existing unary function and turn it into a function that will take a call back.

So in this case we're gonna pass the square root function to continue wise, it will return the square root with a callback function. We will then call that function passing in a function that will receive the result. In this case I'm suggesting that it could be the Alert function, which is something that lives in the browser, which will pop up a box and show you the answer.

If you're not running in a browser you could log to the console or whatever you're doing. Some just some way of seeing where the result is going to go. And then will pass in 81 which is the number we want to take the square root of. So if this function is working correctly, then we should see a box show up on the browser with the number 9 in it.

Okay, so here's continuize, continuize takes a unary function The returns of function that takes a callback and an argument and returns result of calling the callback function with the result of the unary function and the argument. So who got something that works? Congratulations you are now in the 90% tile of JavaScript programmers.

Not that the bar was ever very high but
>> Speaker 2: [LAUGH]
>> Douglas Crockford: But you're up there and it's because you understand an aspect of the language which is really, really important, vitally important that most of the people working this language don't know, don't know that it's even there. And it's really important.

What we've been doing over the last day is a paradigm shift. I could talk for hours and hours about what could happen when you have a function that returns a function and you would think you would understand those words and go yeah but, unless you've done it a few dozen times, it really won't make any difference.

It won't make any sense but you've done it now. When you came in here yesterday morning. You did not know how to write this function. And you just knocked it off. You wrote it very quickly and you got it right. So good for you. And the reason we went through all of this is that I wanted to explain to you again something that I did on the first day that you thought you understood but you didn't.

So, we're going to go through that again. Now and this time you now have the tools of the context in order to understand what I was talking about

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