This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.

Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Reviewing Composition" 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:

Brian Lonsdorf now takes over to discuss "The Voyage". Before moving forward, Brian addresses a few more questions that popped up over lunch about Part 1 of this course. He stresses that functional programming is very declarative and all you are really doing is composing smaller bits to make a larger system.

Get Unlimited Access Now

Transcript from the "Reviewing Composition" Lesson

>> [MUSIC]

>> Brian Lonsdorf: So I was just starting with a little bit of a motivational example of compose. I'm gonna talk more about it, I do wanna say I think this will work. From this point forward, that's our best friend right there. Okay, we're not gonna be using _.compose, because I needed to define different versions of that to work with type classes and categories and what not, kinda dynamic dispatch stuff.

[00:00:33] So don't use the or _.compose here on out. I'm sorry that's so confusing. [LAUGH] I mean use it when you're working with a arrays or functions but don't use it. Yeah, anyway so I just wanted to point out that, so like there are some questions in the chat that we launched about.

>> Speaker 1: Brian you didn't introduce yourself.
>> Brian Lonsdorf: I'm Brian, hey [LAUGH].
>> Speaker 1: What do you do, Brian?
>> Brian Lonsdorf: Let's see, I own a consulting company called Loop/Recur and I teach functional programming as a hobby and I'm glad to be here.
>> Speaker 1: Dr.Boolean.
>> Brian Lonsdorf: [LAUGH] DrBoolean is me on Twitter, check it out.

[00:01:23] So let me just finish up part one here with a quick last question that happened over lunch. Hi Manuel, [LAUGH]. And the point is that you might wanna use this when you're just kind of building up this function. So over here we have our application that's going to first get an array by dashes.

[00:01:48] What does that mean? It's gonna split by dashes and then we're gonna map the first upper. What is the first upper? The first upper is two upper and grabbing the first character, or we could be way more efficient and grab the first character and do two upper. The point is this should be, this should read like a book.

[00:02:11] So the next thing is how about instead of app will make this, getFirstUpperOfDashes, [LAUGH] and then our app is now, this getFirstUpperOfDashes, it's gonna try to auto complete. The composition of that and some other function maybe like take one or take two [LAUGH] take one said. So at that point what we're doing here is building up a function from other functions there's no data going in.

[00:02:49] It's just like a machine that you kinda drop a coin in and when you drop the coin in the coin being this argument that goes in or up over here in the color. Over here it's just functions being combined with other functions compose ability and we're combining functions with data and we're combining functions with functions.

[00:03:07] One of the killer parts of compose is if I ever really long compose, let me make sure this works before I keep talking, there you go. If I'm like you know I didn't want to take to get the head of that all right, and I'll run that and there we go.

[00:03:24] Well the point of this is I've got three functions and I can grab any sub, like any group and pull that out into its own. So I'll make this innefficientHead [LAUGH], inefficientHead, I don't know how JS been works, [LAUGH] we'll just find this first. So what I'm doing here is just extracting method, quote-unquote and into its own composition.

[00:04:00] And now, it still works but the point is that when you glue functions together like this you can kinda just grab the different parts, there's no data, there's no roots deep in your application. You're not depending on your environment in implicit inputs. Everything Joe went over in the first part is leading up to where like we should be comfortable with this right here, cuz we're gonna go further and it's gonna get insane.

[00:04:25] Well looks like better over here, no it doesn't, it looks like the same anyway. [LAUGH] You know what? There we go, yeah whatever. But the whole point is that you make your grammar and then you use it as your application, so what is my app do? Well it gets the first upper of dashes and then it does this inefficientHead on it.

[00:04:49] And that's exactly what it's doing, it's getting the first uppercase and it's doing some inefficientHead on it. And it's very readable and very abstract and high level and declarative any of this can change under the hood. So, and it's okay to use a function if you need to, if this was really hard to write.

[00:05:08] Like just write a function here and do it this way, no big deal. So let's use s substring, and there we are cannot read toUpperCase, so I think I broke that, do this one.
>> Speaker 2: Yeah you got to return.
>> Brian Lonsdorf: Thank you guys, that's what I heard you.

>> Speaker 2: [LAUGH]
>> Brian Lonsdorf: So yeah, so that was just to kinda sum that up, maybe answer somebody's question over lunch about why is this good? We use it and be like, my gosh, this is super declarative and it's reading and like all I'm doing is just composing bits and making this machine that's waiting for, it's like It's like those vending machines where you put in a quarter and you get out a result.

[00:05:55] That's what's happening, where the hell is our quarter and w is our result and it just goes through stuff. Now, there's different, this is kinda beautiful, we wrote apps like this for like a year before we had any idea of what a monad was. And it's JavaScript, it's loose, you don't really need to worry about that so much.

[00:06:12] But there are so many problems with doing it this way, if you don't know how to compose bigger things. And that's what we're gonna talk about in part two. So let's dive into it.