This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.
Transcript from the "No Points" Lesson
[00:00:00]
>> Kyle Simpson: There's another characteristic of the arguments, and how they flow through our functions. And it defines a style of programming called point-free style. For the longest time, I was confused by what point-free style was about, cuz every time I tried to read about it or see it in a conference talk or whatever.
[00:00:19] It was just hopelessly opaque. I could never figure out what are they talking about. And then one day, I was doing this thing in functional programming, I was just doing something that seemed natural to me, and this functional programmer tapped me on the shoulder and he was like, hey, that's cool, that's point-free style.
[00:00:33] And I was like, What? This thing that I've never been able to figure out I just accidentally, what is that all about? So I'm gonna give you the accidentally path. Let's imagine you have this foo that takes a function, it's taking the call back basically. That callback is gonna receive a value of v, and you wanna pass that along to bar.
[00:01:02]
>> Kyle Simpson: See what I'm saying? And then return its output. So imagine that that is the code that you're looking at. And you say to yourself, gee, the shape of that function on line 1 is the same of the shape of the bar function. Why do I need that function wrapped around it?
[00:01:23] Why can't I just pass bar as the call back? Why can't I just make it look like that?
>> Kyle Simpson: Now I just did that as instinctual, I was like, I would recognize places where I had a function wrapped around some other function call, and it was just straight up passing through in the inputs and then returning the output.
[00:01:46] And I just called it like a pass through function, and I was like, just get rid of the pass through function and just pass that directly. I was just doing that instinctually and somebody came along and said, hey, that's point-freestyle. What we mean by points, the way I've been able to gather from my imperfect research here, a point is another way of saying the input to the function, the parameter.
[00:02:11] And what you'll notice here is that we're listing the parameter, and then mapping it directly is the input, the argument to another function. That flow that's happening there is unnecessarily explicit. It's verbose. And point-free style says, get rid of that point. Don't make the mapping between the parameter and the argument explicit.
[00:02:35] Make it implicit by just passing the bar, directly. So that's what we mean by a point, it's that mapping between the parameter and the argument passed through. And to a great extent, a lot of times we can just do something as simple as that, and now we have more point freestyle code.
[00:02:56]
>> Kyle Simpson: Here's another example. There's a special term for a function that returns a boolean value. Any function that takes some inputs and computes a true/false as a result, that's referred to as a predicate function.
>> Kyle Simpson: So, isOdd is a predicate function. It takes the value v, computes the mod of it even if the mod of v mod 2 is 1, then we know it's an odd number.
[00:03:24] Otherwise it's an even number. So I'll gonna find isOdd with that mode operator like I do on line 2, and then if I want an isEven or isEven is just a negation of isOdd. I don't need to recompute mode and compare it to zero or say nod=1. I can just say nodd=1, I've written code like that many, many times before.
[00:03:47] You have two binaries where one is the negation of the other. What do you spot about lines 5 and 6? There's a point there, isn't it? There's a pass through v from the isEven parameter to the v argument of isOdd. That's a point. There's also a negation there, but there's a point.
[00:04:11] So the functional programmer says, how can I get rid of that point? Well, what if I made myself a utility that generally just called a predicate and negated its output? Then I could use that utility to make a point free definition for isEven. That utility I'm gonna call it not, or a lot of times a functional program will call it negate.
[00:04:36] Use which ever term you like. Look at what it does? It takes in a function, returns another one that's expecting any set of arguments, it'll pass those along, but right there on line 3 it negates it. It throws the exclamation mark in front of whatever that result is.
[00:04:54] And now, down on line 11, we have a point free definition for isEven.
>> Kyle Simpson: Is this program point free? No, now we have a point up here in this utility. Where args is passing through. The intent of point free style code is not that there are no points at all.
[00:05:19] It is that most of your code doesn't have points, because many times the points are unnecessary verbosity that just confuses you visually. So it's not that there are no points, it's that we collect those points into well known, established, provable, verifiable utilities, leaving the rest of our code free of those points.
[00:05:39] That's what point-freestyle is looking for. It's not really changing any of the functionality of the program, it's just a stylistic choice that we make, to try to make the style, the readability of the code a little bit easier. A functional programmer will recognize not, or rather negate. They know right off the bat exactly what that's doing.
[00:05:59] It's gonna flip the parity of a predicate. I don't need to worry about that. I know what that does. I don't need to think about it, and I certainly don't need to be confused by those points.