Check out a free preview of the full JavaScript the Good Parts course:
The "Problems 10-12" Lesson is part of the full, JavaScript the Good Parts course featured in this preview video. Here's what you'd learn in this lesson:

o Problem 10: Write a function twice that takes a binary function and returns a unary function that passes its argument to the binary function twice. o Problem 11: Write a function compseu that takes two unary functions and returns a unary function that calls both of them. o Problem 12: Write a function compseb that takes two binary functions and returns a function that calls both of them.

Get Unlimited Access Now

Transcript from the "Problems 10-12" Lesson

[00:00:00]
>> [MUSIC]

[00:00:05]
>> Douglas Crockford: Here's an easy one.
>> Speaker 2: [INAUDIBLE].
>> Douglas Crockford: [LAUGH] We're starting to get a little closer to useful things. Write a function twice that takes a binary function and returns a urnary function that passes its argument to the binary function twice. So, twice add is double and twice mul is square, okay?

[00:00:31]
>> Douglas Crockford: And twice subtract would be zero and twice divide would be one.
>> Douglas Crockford: Does anyone need more time?
>> Douglas Crockford: All right, so here we go. Function twice takes a binary function and returns a function that takes an argument, and returns the result of calling the binary with that argument twice.

[00:01:04] Who got this one? Outstanding, that's really good. Anybody struggle with this one? Yeah?
>> Speaker 3: [INAUDIBLE]
>> Douglas Crockford: [LAUGH] But you got it that's great. Any other discussion about this one before we go on? All right then here we go. Write a function composeu that takes two unary functions and returns a unary function that calls them both.

[00:01:37] So here we've got composeu, we pass it double and square. And past result of that three and that should be 36.
>> Speaker 4: I have a question.
>> Douglas Crockford: Yep.
>> Speaker 4: In real life, it seems like the advantage of the solution of this is the runtime result with the value could be a string of numbers or something.

[00:02:07] Because you have the same function. Do you use that a lot in practice?
>> Douglas Crockford: Generally not. I'm pretty strict on what's going to be a number and what's going to be a string.
>> Speaker 4: So when you write your functions, it's usually going to be one or the other?

[00:02:19]
>> Douglas Crockford: Yeah. Yeah, JavaScript was created around a time when there was a fashion in scripting languages to try to ignore the difference between numbers and strings. And I think we've learned that that was a bad idea. So JavaScript sort of stuck in the transitioning between those two ideas.

[00:02:44]
>> Speaker 4: [INAUDIBLE] versus a-
>> Douglas Crockford: Yeah exactly. Basic I think actually got it right, and there are some people who thought Basic's being too strict. We can relax it. It turned out relaxing it like HyperCard, for example relaxed it. And it actually introduced a lot of problems doing that.

[00:03:01] Because it creates an equivalence of the two types in the programmer's head. But the two types are not always equivalent, and so there's some places where you get confused.
>> Douglas Crockford: All right, anybody need more time? Okay, so here we go, composeu takes two functions, f and g and returns a function that takes an a and returns result of f on that result and g on that result.

[00:03:33] So who got this one? Fantastic, who struggled with this one?
>> Douglas Crockford: So is there anybody who's not getting these who needs some advice or direction or focus, job counseling, anything? All right, should we move on? Okay, write a function composeb that takes two binary functions and returns a function that calls them both.

[00:04:05] So here we have composeb taking add and mul which returns the function that takes 2, 3 and 5 and returns 25.
>> Douglas Crockford: Does anyone need more time?
>> Douglas Crockford: All right, here we go. Composeb takes two functions, f and g, and returns a function that takes a, b, and c, and returns f(a, b), passing that to g with c.

[00:04:43] Who got this one?
>> Douglas Crockford: Very good. Any comment about it?
>> Douglas Crockford: So, the thing was starting to do now is you compose functions, making functions out of existing functions. So, you can start to see how you can take primitive things and start to build them up to make complicated things.