Hardcore Functional Programming in JavaScript

# Composition Exercise: Challenge 3 Solution

Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Composition Exercise: Challenge 3 Solution" Lesson is part of the full, Hardcore Functional Programming in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

## Joe concludes the composition exercise with the solution to challenge 3. - Problem: http://jsbin.com/jevag - Solution: http://jsbin.com/gowugugaso

Get Unlimited Access Now

Transcript from the "Composition Exercise: Challenge 3 Solution" Lesson

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

[00:00:04]
>> Joe Nelson: But as far as number two, are we solid on that, aside from maybe it's not the best use for a compose?
>> Joe Nelson: No. [LAUGH]
>> Joe Nelson: So, I'm trying to think, so, do we need an explanation again of, how does isAuthor work? That's actually right.
>> Off Camera 1: People like the fork example, if you can go over it.

[00:00:36]
>> Joe Nelson: Okay, cool.
>> Off Camera 1: It is cool to be like, whoa, you can do that?
>> Joe Nelson: Okay, so we want to take an average. And compose is kind of a pipeline, where just using compose, it would be hard to take the average. Cuz the average, you've got a list of numbers and you need two facts about it, like the sum and the length.

[00:01:00] And you think, okay, I'll start composing. Like, I get my list of numbers, which one do I want, how about the sum? And then whatever you're going to feed that into, you're kind of stuck, because you don't have the length anymore. You have no idea what it was that was fed into the beginning of the compose.

[00:01:13] So there's this fork thing, it says, fork, you give it two functions, and I think a combining function lastly. Yeah, yeah, yeah, so you say, okay, there's a fork in the road. It's like compose, the values come in, they get into fork, and fork says, I've got two functions.

[00:01:30] I'm going to call this value that just came in on both the functions, and then take the results of what those functions gave back and give it to lastly, this function called lastly. It's up to you how you write it, so all these three things are up to you.

[00:01:46] The two functions that you want to do on the input and how you combine it with lastly. So knowing that's how fork works, let's see, the order it takes is lastly, f, g, and then the mystery x. In fact, it's so much easier just to look at it now that I think of it.

[00:02:01] Look at what it does. Fork, three functions and a value. It puts that value into both the functions, and sends that into lastly. So if you have it in your tool belt, this fork thing, you never have to, or I think there's less variables you have to write in the future, you can just apply it.

[00:02:22] So what would average look like? The very worst way I can imagine, the most procedural way is, I have a counter, I have a loop, I add up. I guess I could reduce, I could use sum, that's a little better. And I do a couple steps and I return it.

[00:02:36] But we could just fork, fork. So lastly would be, I forget if it divides, we may have to flip it, or divide by. There's two different ones to say, the order that the arguments come in, is it the numerator or the denominator. We'll find out, or it'll be in the chat.

[00:03:06]
>> Joe Nelson: Trying to think. So one of the arguments would be composing the length with divides.
>> Off Camera 2: Wouldn't your lastly function be divides? It's the last thing you want to do. And then your two functions after that would be sum and then size.
>> Joe Nelson: Sum and size, totally.
>> Off Camera 2: You wouldn't be passing those into divide, the formula does that for you.

[00:03:27]
>> Joe Nelson: Nice, just divides.
>> Off Camera 1: [INAUDIBLE]
>> Joe Nelson: What's that?
>> Off Camera 1: It's singular.
>> Joe Nelson: Divide, length, or is it size?
>> Off Camera 1: Size, sums, actually, first, and then size. Nice, very easy.
>> Joe Nelson: That's kind of pretty, I don't know. I guess maybe fork is pretty cool if it works. All tests pass.

[00:03:54]
>> Off Camera 3: What is, if you go up to fork, the definition. That is the function instance of functor or applicative functor? So it does actually really come in handy. [LAUGH]
>> Joe Nelson: You're saying this has a more general name, and this is used in other places?
>> Off Camera 3: Yeah, if you think of f and g as a functor with something inside, and to get it, you have to run the function.

[00:04:22] Then, we'll look at it later, but you run both functions to get the applicative functor. And then you apply that lastly. So you lift lastly and you apply it to the first steps to get the value out. And then you apply it to the g to get the value out and you get [CROSSTALK].

[00:04:36]
>> Joe Nelson: So most likely, no one has to write this thing.
>> Off Camera 3: Yes.
>> Joe Nelson: It's part of the monadical sort of applicative stuff you're going to do later with Brian's journey.
>> Off Camera 1: Yeah, I don't know if I'm going to cover the function instance of applicatives, but that's it.
>> Joe Nelson: [LAUGH] That's it.

[00:04:52]
>> Off Camera 1: It's also the S combinator, I think? No.
>> Joe Nelson: Why don't we pass the value x in for fork? Because fork is curried, so it doesn't require everything to be, I think, let's see.
>> Joe Nelson: Yeah, yeah, yeah, so in the test, we say assertEqual 3 to average of something.

[00:05:24] Now, average, as it stands, is forked of this stuff, but it's curried, and it hasn't been given all the arguments it needs yet, so it's a new function waiting to get it. That's why we didn't have to explicitly, yeah, exactly.
>> Joe Nelson: I'm assuming everyone's reading the chats as well as I am, so I'm like responding to a ghost.

[00:05:48] And you're like, what's happening? [LAUGH]
>> Off Camera 1: [LAUGH]
>> Joe Nelson: Okay, so those are the three, and those are their answers.