Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Point Free Solution" 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 live codes the solution to the exercise.

Preview
Close

Transcript from the "Point Free Solution" Lesson

[00:00:00]
>> Kyle: Alright, let's talk about the solution for this point free exercise. As you read in the readme, hopefully you have three functions output, printIf and isLongEnough. Let's just go in that order. We'll start with the output function. Using our fancy technique of equational reasoning, we can see that the output function, which takes a single string.

[00:00:23]
Has in this case a compatible shape with the log function. Ostensibly it would seem that we could just say output equals console.log and that should be our point free definition. There's one little hiccup depending upon the JavaScript environment that you may be running in. Which is that the log method expects to be called specifically against a bound object.

[00:00:48]
And so if you're doing this indirect assignment, you may need to do a hardbound version of it to the console log to the console object.
>> Kyle: So that's a point free definition for output. Let's skip down to isLongEnough. Recall in our discussion we talked about the not function, here's how that one looked.

[00:01:12]
We said not, it takes in a function, it returns back a function that's been, that we'll call negated. Which receives some arguments and when those arguments have been received, it calls the original function with those arguments but then it negates the result.
>> Kyle: If we had that utility, then isLongEnough should be expressed in point free as isLongEnough = not(isShortEnough);

[00:01:59]
>> Kyle: And there's our point free definition for isLongEnough.
>> Kyle: Now the printIf function is going to be slightly more complex to explain, but we need to go back to our hints. We talked about a utility which you would be able to expect from many of your functional programming libraries.

[00:02:21]
This one is called when [COUGH] it's referred to by different names but it is essentially a functional if statement. It makes a decision to call a function only if something else is true. So the function that we wanna call is the first input, and then the test that we want to perform to see if we should call it is the next input.

[00:02:45]
And I've named that here predicate because that is a special term for a function that is going to return us a true false value. So predicates are functions generally unary functions that return a true false. So we're going to have a predicate function that performs our test. And if it passes, then we're gonna call the initial function with the arguments that were passed in.

[00:03:11]
It's kind of like an IF guard around a function only call it if this other predicate passes true. So I have manually curried this, that's a technique that we'll talk about later in the course this idea of currying. I've manually curried it to receive these three inputs separately and if you look at the bottom of the EX, you'll see that kind of happening the same with the printIf function.

[00:03:35]
The way the printIf function was designed, it also receives inputs separately. And that's gonna be a hint towards our solution, we need to use some equational reasoning. So, I'm gonna switch to a separate file, and we're gonna talk about this equational reasoning. Let's start with the printIf function.

[00:03:56]
>> Kyle: And then, let's start with, let me copy the when function from our readme. We're gonna put these alongside each other so that it's easier to see what's happening. You recall at the bottom that we call printIf, for example, we call printIf, with isShortEnough and then message one.

[00:04:21]
How would we do it if we were going to use the when utility? What would that look like? Well, we would call when, and what do we need to pass to when? We need to pass it the function that we want to execute if the predicate that we later pass,that passes.

[00:04:37]
What do we want to have happen in the case of it succeeding? We want to print it so we need to use the output function. And then what would we call? Well, the next thing that's needed is the predicate. What's our predicate? It's the, isShortEnough and then, what arguments need to be passed along to isShortEnough?

[00:05:05]
Message one. So, these two are going to give the equivalent output.
>> Kyle: Let's try to use our equational reasoning skills to figure out what is the point free definition for printIf? If I use some white space in here, hopefully it becomes obvious to you. That when of output is returning a function that has the same shape, the same kind of signature as the printIf function.

[00:05:40]
So our printIf point free definition would be when about. He could have done that equational reasoning at the call site the way I did that and that's the way I like to do it. The other way to do the equational reasoning would have been to look at the function signatures.

[00:05:58]
To look at the fact that this function takes what is essentially a predicate, and then some value to pass into it. And that looks the same as this function and this function, those two have the same generic shape. So then you would have been able to see if I just call when with an fn, I get back a function that is the same shape as printIf.

[00:06:23]
That is the other way of applying the equational reasoning. We take that back to our exercise file and printIf can be commented out. And here's our point free definition for printIf. We can copy in the when as well just for a good measure.
>> Kyle: Collecting those three at the top just to make sure that we see all of them.

[00:07:10]
>> Kyle: And there is our solution for this exercise on point free style.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now