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

A function that does not change anything outside of itself is considered a pure function. Pure functions react the same way to the same inputs. They are more testable, portable, memoizable, and parallelizable. Joe plays a game called Pure or Impure to help the audience recognize pure functions.

Get Unlimited Access Now

Joe Nelson: And the next bit is recognizing these types of functions that are really useful, like the days in month as a pure function. A function that doesn't change anything outside of itself and reacts the same way to the same inputs, they're called pure, and what does it make them?

Well, we've just been beating it into the ground. It makes them more testable, as Brian mentioned, it makes them more portable in the case that they don't actually depend on some browser thing. You could use them on the client in the server. It makes them memorizable, and memorization is a technique that once you run a function one time with a certain input, a little thing watches over and says okay, I'll remember that.

Next time someone tries to call this function without input, I won't even ask the function, I'll just give it the input. And like in Haskell and in other places, and I guess, theoretically, in JavaScript too, you could memorize these things without having to think about it. So in the cases where you want to make that space versus time tradeoff in your programs, you don't have to change, your function's already pure.

And finally parallelizable, the tongue twister word, so because the things don't change anything, it doesn't matter which ones you're running at what time. If you have different processors, you can run as many as you want. They're not going to step on each other's toes. So do we really understand what purity means?

Let's play pure or impure. I guess we have an advantage here with the people that are present because we can shout it out. So here's a function, getQueryVariable. You give it a variable, and the idea is, if I was in a page like items question page equals three, and I say give me the page, like the query string parameter, it should give me three.

So I give it the variable. It checks and it gives me, pulls it out of the url and gives it to me. What's your guess? Pure's good, by the way, so I'll put thumbs up for pure. Kill him for impure.
Joe Nelson: You're feeling thumbs down? Why do you feel thumbs down about this one?

Speaker 2: I don't want to be the one to answer that. Does anybody else want to-
Speaker 3: Windows location.
Speaker 4: Windows locations then?
Joe Nelson: Yep, it's looking, it's looking at things it shouldn't look at. You're not pure. Next, okay, here's one, random numbers. This thing generates random numbers. Who knows what comes out of that?

We give random numbers, like these seed values, to kick it off, and it gives you a random number, kind of complicated in there. What do you think about this one? You're happy about it. So you're not happy about it? A lot of happiness. It's making us all joyful.

It's pure, because although it's doing complicated things, it's doing them in a well defined way. It's just like some number crunching, and you give it the two numbers. And it's kind of crazy as a side note that this thing actually generates fairly statistically spread out numbers based on what you put into it.

I was playing, nudging them by one or two, and I was getting drastically different numbers, it's a bizarre algorithm. So pure or impure, the final round, chattyAdd. It adds numbers, but you kind of wanted to know what's going on because you're balancing a checkbook or something. So it's telling you what just happened, so you know what's going on.

Feels good? You gotta feel like Caesar here. I want to see more thumbs, should this thing live or should it die? Live. If I set a really pure function, one that we can really count on, it could be parallelizable, I got it that time. And it doesn't matter when they're happening, the result will look the same from the outside world.

And if this thing's logging and if you ran it in parallel, you wouldn't know which order the logs might come in. So, it's not pure, it's changing something. The log, it's something, it's out there. It's outside of the function, and it just went out there and did it.

Speaker 2: People in chat are kicking butt. They're all like, I know, impure, done.
Joe Nelson: We got the pros.
Speaker 2: Yeah.
Speaker 5: Although wouldn't it matter what console actually does? You're assuming you know that console is a impure function.
Joe Nelson: That's a good point. It's like dependency injection, yeah.

Speaker 5: Yeah.
Joe Nelson: Maybe the takeaway would be that, if you compose pure functions, it stays pure. But if one function ever relies on another, it'll become impure as its argument was impure. So purity begets purity.
Speaker 5: So would this function actually be pure, what you're arguing is it's the input that makes it impure.

Joe Nelson: Yeah, you're right.
Speaker 5: because you're assuming console is impure.
Joe Nelson: You're right.
Speaker 5: Okay.
Joe Nelson: I guess I assumed it was in a browser, and it passed in the console, but it doesn't have to be the case. It could be something that just throws it away, it would be very impure, very pure.

It gets your computer hot or whatever.
Speaker 5: Yeah, it warms it up. Well, I don't necessarily know what console log does.
Joe Nelson: Yeah, exactly, it could be something innocent as a flower.
Joe Nelson: So you just played our game

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