Check out a free preview of the full Functional-Light JavaScript, v3 course:
The "Equational Reasoning" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle describes how equational reasoning can be used to determine whether two functions have compatible shapes, and therefore whether they are interchangeable with each other. - http://static.frontendmasters.com/resources/2019-05-06-functional-light-v3/functional-light-v3.pdf

Get Unlimited Access Now

Transcript from the "Equational Reasoning" Lesson

[00:00:00]
>> Kyle Simpson: All right, so let's take a look at another aspect of functions. And this is kind of a higher-level concept and it may twist your brain a little bit, but that's okay. That's what we're here for, we wanna push ourselves, so stick with me on this. Point-free, what is that?

[00:00:12] I'd heard this phrase many, many times when I was trying to kind of pick up functional programming. And it sounded like one of those fancy concepts that I didn't know what to do, and didn't know what that meant. But then I learned about it, and I was like, well I've kinda already been doing some of that.

[00:00:28] And that's one of the gratifying sort of takeaways from a lot of functional programming, is that it turns out that this big idea of functional programming is built up by a bunch of tiny little things, most of which we've already done, or at least we can find intuitive.

[00:00:45] It's rarely the case where you look at something and say, that is just so completely out of the realm of anything I ever would have thought or done that it just totally mind bends. It's the combination of all those things that really pushes us into this different paradigm.

[00:00:59] So we're gonna talk about this style of writing our functions. And this may sound a little strange, but it's a way of defining a function without actually writing anything in the function. It's a way of making a function by making it with other functions. That's really what point-free style is about.

[00:01:15] So what does this even mean, what does that name even mean? Well, I don't know if any of you remember this. I have a very vague recollection back to my seventh, eighth, ninth grade time frame, something like that, long time ago. I remember a teacher saying something to the effect of fixed point functions, sometime when we were doing something with our graphing calculators or something.

[00:01:37] And I don't remember what they meant by that. I've since googled it and looked it up on Wikipedia and I sort of now come to a better understanding. I wish they'd just said at the time more clearly, or I'd been able to understand more clearly, what it was.

[00:01:51] But the idea of a fixed point function, remember back to our initial example. We were talking about, we were motivating at the beginning of the course, the f of x and the two x squared plus three. Remember that function? Well, the idea there is that the input x outputs a y.

[00:02:08] What if that function had multiple inputs? What if it took an x and a y and it outputed a z? Well, now we'd have like a three dimensional, it'd be a two dimensional surface in three dimensional space, right? So,
>> Kyle Simpson: The idea of a fixed function, is that if you have more than one input, you take one of the inputs and fix it at a certain value.

[00:02:33] So for example, I could say take a function that takes an x and y and produces a z, and fix the x at the number three, for whatever reason. Now that's gonna tell me the line on that surface at exactly x equals three. That's mathematically, what that was all about.

[00:02:51] So fixed point functions were where we took one of the inputs and we fixed it at a certain value. Well, that's sort of speaks to why we're calling this point-free, because point-free definition is defining a function without needing to define its points, aka, it's inputs. Without needing to explicitly define and map its inputs, can we define a function?

[00:03:17] And in a sense it really ends up as a class or a family of techniques for defining functions by way of other functions, okay? So let's take a look. Some of you have probably done something like this, many of you, have passed in a callback, for example to this getPerson utility.

[00:03:37] I pass in this callback that receives line 1, an input-called person, and it passes that along to some other function, in this case to renderPerson. Anybody seen code like this before? Seems somewhat familiar, okay? So this is very, very common, we receive inputs in one callback. We pass that input to another callback.

[00:03:59] That person parameter there is the point, it is the input. In mathematical terms, it's called a point, for the onPerson function. And you notice that we've defined the onPerson function by specifically and explicitly listing out that point. So what if we could define the equivalent of that function without needing to list the point at all?

[00:04:24] Some of you may have actually done this before. And this is where when I was first learning point-free I was like I've done that before. I just didn't know that it was called point-free. You might notice that the onPerson function taking a single input, and the renderPerson taking a single input, you might notice that they have basically the same shape.

[00:04:44] Which means if they have the same shape, then they're interchangeable. Which means that I could just pass renderPerson as the callback. Anybody ever done something like that before? Where you had this wrapper function and then realized, I don't need the wrapper, I can just pass in the function.

[00:05:02] Well if you've done that before, I have good news for you. Without you even knowing, you were doing this really, really cool thing that I'm gonna give you this term, and you're gonna be able to go back to your teams and use this term, and you're gonna sound really smart.

[00:05:16] And everybody's gonna be like, whoa, that was really cool. What you were doing was called equational reasoning. That sounds cool, doesn't it? I'd like to put that on my business card. I'm an equational reasoning master.
>> Speaker 2: [LAUGH]
>> Kyle Simpson: It sounds cool, it's actually pretty mundane, okay? What's mundane about it is that we're basically saying, if I have this thing that's got this one shape, and I have this other thing that has the same shape, even if they do different things, if they have the same shape then they're interchangeable.

[00:05:52]
>> Kyle Simpson: Equational reasoning means reason about this thing and say that it is equationally not equal, equal in identity sense, but equal shape sense with this other thing. Here we have an onPerson function that is equationally, it is equivalent to, the renderPerson function, which means that we can pass renderPerson.

[00:06:15] And the takeaway here is that we were able to get away with defining a function without needing to list the point. That's one aspect of point-free, but I said it's kind of a family of techniques. So there are other things involved that fall under this heading of point-free.

[00:06:34] And before I even show you some of these other more advanced techniques, I just wanna warn you. It's like what I talked about in the intro to the course, when I said that you'll be trying something, and all of a sudden you'll cross this line from I was tracking with you to, what did he just do, right?

[00:06:51] At some point in the next slide or two, we're gonna cross that line for all of you. It's okay, we definitely did for me as well, okay? But before I even show you though, what I wanna warn you is, is if you see this and then you say, as a response, well I don't get that, that's not more readable.

[00:07:09] That's weirder to me, so I'm not going to do it. That's not the best response. A better response would be, wow, that's not what I was expecting. And it's going to take some time to get used to that, I should try that a bunch more, okay? But on the other hand, another response, and I have seen this from folks, including myself, is that you see this and you're like, whoa, that's so cool.

[00:07:38] I'm gonna do that everywhere, I'm just gonna point-free everything. And you very quickly explode the complexity, the surface area of that complexity, way beyond your capacity to even understand what it is you've written.
>> Kyle Simpson: So that's also not a great healthy response. The healthy response here is to push yourself a little bit, but fall back to things that are practical for you.

[00:08:04] And then push yourself a little bit more and fall back to what's comfortable and practical and readable and reasonable. And do that little by little by little. What I'm about to show you is the first time I saw it I was like, I don't know about that. And then after a while that became comfortable and then my line moved a little bit.

[00:08:19] And then I tried a few more more advanced techniques in point-free and my line moved a little bit. And then I tried a bit more, and a few months later, the line moved a little bit. So that's the way it's gonna be for you, and particularly with point-free, don't go overboard.

[00:08:34] Don't go overboard with it, because this is that point where my readability curve started tanking. It was with point-free, I was trying it cuz it was so cool. And I way over-blew the complexity of it and lost all the benefits. At the end of the day, if your code is not more readable and understandable, then it's not benefiting you.