Hardcore Functional Programming in JavaScript Hardcore Functional Programming in JavaScript

Composition Exercise: Challenge 2 Solution

Brian Lonsdorf
Salesforce
Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Composition Exercise: Challenge 2 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 walks through the solution for challenge 2. He also gives the audience a little time to work on the third challenge. - Problem: http://jsbin.com/jevag - Solution: http://jsbin.com/gowugugaso

Get Unlimited Access Now

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

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

[00:00:04]
>> Joe Nelson: So what's our challenge here? Make a boolean function that says whether a given person wrote any of the articles. Okay. So we already in the previous one made one that shows us everyone who wrote articles. So we can use to build on that I guess, would be one way to think of it.

[00:00:23] Was our first one, actually we already defined it, it's called name, so I guess we could use it here. See up at the top we have the solution to challenge one is a function called names, that'll tell us the names of the people who did the stuff, who wrote the stuff.

[00:00:42] So I think we could say contains. See we need to get the array put into a thing. That's a good point-free way to do it. Compose in contains. Yeah, we could compose(_contains) okay, I don't know I think you're writing. Let's refractor, let's start like this function, name, and say return compose, contains name, names.

[00:01:32] It's got to be a way better way. Just thinking it through.
>> Student: Hey Joe or Brian could we get elaboration on what point free code means?
>> Joe Nelson: Sure I'll go back to a slide
>> Student 2: Could we finish two first before we get to that? That's good from there.

[00:01:51]
>> Joe Nelson: Alright, finish two. So, let's think how to make it pretty. I'm thinking a composition.
>> Student: I don't think there's a way to do this one.
>> Joe Nelson: Wait, actually, we could take out the article. Because what compose returns as the function is waiting for what articles the name is expecting.

[00:02:13]
>> Joe Nelson: You don't think there's a point three way of saying?
>> Student: No because the name I see going to contains the really good if it's really good if you try to if you don't make a commentator you do this you're just going to end up with weird crazy as you just read the function of Definitely.

[00:02:29]
>> Joe Nelson: So let's see before i press run. It just seems to make sense. It is authored, you've given it a name. It's going to return a composition scale. It's going to return a function which does names first on whatever it is given. So that means because names is the last thing in compose.

[00:02:46] That means that's the first thing that new arguments go into in the whole composed result. So names was expecting this kind of object thing we had before. Which was articles.
>> Student: [COUGH]
>> Joe Nelson: Cool. Let's see if it works then we'll go over it again.
>> Student: You're gonna have to call it in a weird way, because if the returning a function, isAuthor returns a functions.

[00:03:15] So, you have to close it and reopen it when you call it.
>> Joe Nelson: That's a pain.
>> Student: Yeah, I would mention that part.
>> Joe Nelson: Just do it. And at that point.
>> Joe Nelson: 25 seconds and I could
>> Student 2: You are returning the function instead of the result it contains.

[00:03:55]
>> Joe Nelson: Yeah I should do this. I should like-
>> Student: There you are.
>> Student: Good.
>> Joe Nelson: Yay, new errors. How do we play this up like is there a way that seems a little bit complicated?
>> Student: The way we're dealing with the would be composed.
>> Joe Nelson: I thought like when I originally made it such a safe.

[00:04:18] What the answer might come out lies it was because I understood.
>> Student: I mean I put it on one line. It seemed pretty straightforward to me. It was like run this composition.
>> Joe Nelson: Okay, well it does use composing and contains. And maybe I make a big deal of it because its not as point free as I want to.

[00:04:46] So is author, you give it a name, you give it articles. It does a couple things. It pulls the names out, it sends it into contains with name, and so then that's it.
>> Student: We're going to see a whole bunch of compose. And it's gonna feel really useful throughout the rest of this.

[00:05:13] But, if you're having some trouble right now understanding why, what it does, ask questions. And if you're understood if you like why is this important and why do I care, you will just see a bunch more examples to answer that second question.
>> Joe Nelson: Yeah, every challenge to release at least I answered is not a compelling way I like yeah compose uses everywhere like when I was calling like pass one function in another.

[00:05:36] But it's absolutely useful all the time, like in effect, in a live demo that we're gonna make, it's used a lot ineffectively.