The full video and many others like it are all available as part of our Frontend Masters subscription.

Brian Lonsdorf

Brian Lonsdorf has taught several workshops and training courses on functional programming with javascript. He'a a regular speaker at conferences and helps organize/host FP events around the Bay Area.

Brian Lonsdorf

Hardcore Functional JS

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

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

So don't use the or _.compose here on out. I'm sorry that's so confusing. 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.
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: DrBoolean is me on Twitter, check it out.

So let me just finish up part one here with a quick last question that happened over lunch. Hi Manuel. And the point is that you might want to 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.

What does that mean? It's going to split by dashes and then we're going to 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.

So the next thing is how about instead of app will make this, getFirstUpperOfDashes, and then our app is now, this getFirstUpperOfDashes, it's going to try to auto complete. The composition of that and some other function maybe like take one or take two 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.

It's just like a machine that you kind of 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.

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.

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 inefficientHead, I don't know how JS been works, we'll just find this first. So what I'm doing here is just extracting method, quote-unquote and into its own composition.

And now, it still works but the point is that when you glue functions together like this you can kind of 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, because we're going to go further and it's going to get insane.

Well looks like better over here, no it doesn't, it looks like the same anyway. 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.

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.

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.

Brian Lonsdorf: So yeah, so that was just to kind of 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.

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 kind of 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.

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 going to talk about in part two. So let's dive into it.

Ready to take your code to the next level?

Intense courses with world-class teachers and unlimited access to our growing library of videos for the great price of $39 per month.

Get Unlimited Access Now