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 walks through the solution to exercise 2 and answer a number of follow-up questions from the audience. He also further emphasizes how implementing a map on a container abstracts a function's application.
- Problem: http://jsbin.com/yumog
- Solution: http://jsbin.com/xiwofojura

Get Unlimited Access Now

Brian Lonsdorf: Let's go over two and then let people do three, basically, just kind of did it but let's look at this. Let me make sure if I run it, remember not to find. Good. All right. Undefined is not a function because I have not defined the function, let's do that.

So what my test is hey, I'm actually expecting identity of do, which is the first thing in this list. And I'm going to run it with xs, which is an identity of this list, right? So, you don't have to think too much about it. We want to say, use head to get the first element of the list, but we're going to map over identity to do that.

So, we're going to map head over the identity. Well, identity gets passed in. Is that what's tripping you guys up?
Speaker 2: It was tricky for me
Brian Lonsdorf: Okay.
Speaker 2: I tried with actually using
Brian Lonsdorf: Yeah, that makes a lot of sense. The reason I wanted to do this exercise.

They're all making functions that are not actually running functions and I'm running the function in the test which I probably should have thought about a little bit more, but what's cool about this is it doesn't have to take identity. You can take maybe, you can take a list any functor.

So of course, the functor has to have a list inside it or else have a blog but mapping over any functor, map is just going to dynamically dispatch on any functor it gets. So, when I give an identity of this it'll work. So, let's run that. But yeah, I could totally understand why that tripped a lot of people up.

What did I do wrong? Map head, great, yeah. Sorry about that. Did I not do one? We're going to do one together, why not? Sorry guys, did I miss that?
Speaker 3: No, you did one already,-
Brian Lonsdorf: Okay.
Speaker 3: And then you blew it away.
Brian Lonsdorf: All right. That's right, okay.

So we've got one in CU Ged. So, we're just going to map out one with the map head. It's really not that difficult, it's just like working with lists, but it's a different type, and you're like why does that work? I don't like it. Okay, got number three, I like it.

So for a lot of people online, I don't know if there's a lot of people in the chatroom or what, but if you're out there and you're confused I'm here now and paid. So, ask me. Questions or Joe? Wait another minute or two before we do three. And then, how many exercises are there?

Students: Four.
Brian Lonsdorf: Okay, cool. I think one of them has like six. We don't have to do them all. Yeah.
Brian Lonsdorf: So we're using currying partial application of map here with head, so I could write this another way to say
Brian Lonsdorf: And that'll get me,
Brian Lonsdorf: That'll get me an identity a blah, but up here we just partially applied it without calling the whole thing and they could just use xs which is an identity of this list.

So, this will actually run it. This will return me a function that takes an identity or some factor.
Brian Lonsdorf: Take this opportunity to talk about how much I love the Gin Blossoms.
Brian Lonsdorf: Just telling Joe, came on in the grocery store the other day, I was like, yeah and now I hate what I've become,

Brian Lonsdorf: James, head is in ramda and we've been using ramda this whole time. There is maybe one other function we might see from it that hasn't been in the other examples. Sorry for not going over that, it's just like first, you have to remember creators in the chatroom.

Brian Lonsdorf: I have a 20 second delay, right?
Speaker 3: Yeah.
Brian Lonsdorf: All right
Speaker 3: So when you see they're chatting, they're going to hear your answer.
Brian Lonsdorf: Do that, don't do that. 20 seconds later.
Speaker 3: Someone's asking, what is
Brian Lonsdorf: Well, let me talk about, for the people who are bored and, well other people are working, there I'll ask theoretically here.

Map lifted head into a functor context. You've raised this function, they just worked on something normal into something that worked on a functor, so we've transformed this function into a new function. That's kind of cool, just like a list, if I have a function that just gets like I don't know, running out of cheap easy examples.

But let's say, okay, let's just replace a's for b's and var abs. That's absolute value isn't it? A4b, take it's s. I don't know, ramda get on it. Why isn't this in there? Should be there. Anyway, so a4b, this just works on a string, right? If I wanted to work on an array, I could lift it into an array function by just boom.

Now, a4b's works on an array not just one value. We've broken the problem down into each element we're just going to run it on the one thing and then we'll just map over it. If we need to, now we've got double purpose. I could just call this in line.

A lot of people don't do this right now because they have the ceremony of the glue code in the names in the return in the blah, blah, blah. I don't know, it's something like Bill Cosby and your jello. But the point is that map, if you just surround a function now, you just partially apply it.

Boom, now, this function works on anything.
Speaker 5: That could work on maybes?
Brian Lonsdorf: That works on maybes, identities, anything. So, hey, I need a null check. Wrap this thing in something, now it's equipped with nulls, null checks, if it gets a null, maybe. It works on functors. Depending on the functor, this will run many times, this might not run.

This will run with no effects at all.
Speaker 5: So when you use the term lift, you're just saying take a standard function and make it work with any form.
Brian Lonsdorf: Yes. Yes, sorry, someone, Mark G's talking, I always have to pay attention. So, that's exactly right. Look at the implementation of map on Container.

Look at that, where did F go? It went inside of the container. We've lifted it into the container and ran the function from within the container. Same thing with maybe. Let's look at maybe. Hurry, hurry. But this one is more interesting, so one would look at this one and say, we've lifted capitalize into the maybe context to run it there it doesn't run here.

It doesn't run capitalize on flamethrower, it runs capitalize on flamethrower inside the maybe we've lifted it in there. So the power of running a function went from within a context, allows you to abstract function application. You can do all sorts of crazy stuff. You can do type conversions and you can run many times like a list.

You can do maybe, maybe not. Now you have to obey the laws, which we will look at and that kind of restricts what you can do here. But when you get the idea of map, you get a pretty good intuition of it's map, I'm just going to go in there and map it.

So, if you're working in Schola and you get an option. You're like I want the thing inside the option. You can't get the thing inside the option. You have to map over it. So yeah.

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