Transcript from the "Challenge 2: Solution" Lesson
>> Kyle Simpson: All right, let's chat about exercise 2. As we see in the Read Me, it indicates that you should refactor the output, print, if and is long enough functions to be point freestyle. I've given you some hints here on how to approach that. First we saw that console.log was inside of the output function.
[00:00:21] So if we look at that here and you see console.log is inside of the output. There is a point. You might think I could just call console.log output. I could just say output equals console.log. In general, that may work in various browsers if you test it. But there are some browsers where that can create a problem because they expect for the log function to be run in the context of the console object.
[00:00:44] And because of this binding rule, if you don't do that, it will end up not working correctly. So the safest way to do so is to do console.log.bind. So, I can very easily say, var output is equal to console.log. But instead of just saying console.log, I can do a hardbound version of it to the console object.
[00:01:09] And that's safely lets me, I have an output, so there is now a point free version of the output function. Then we have printIf and we have isShortEnough and isLongEnough. I'm sorry, we have isLongEenough, that's the one we wanna re-factor. So I'll move down to that one first.
[00:01:30] We notice that this was a negate and we saw in our previous code, I mean in our previous lecture, that we have a utility for that called nod. So if we make a nod utility, I'm just gonna, since I don't need to retype it, I'm just gonna copy here from the solution.
[00:01:47] But if we made a nod utility or we call it negate, isLongEnough can simply be defined as the not or the negation of isShortEnough. So there's a point free definition for the isShortEnough function.
>> Kyle Simpson: And now the printIf function, to refactor that one to be point free, we need a slightly more complex utility and I provided you one called When.
>> Speaker 2: [INAUDIBLE] The wrong function.
>> Kyle Simpson: Did I? Sorry.
>> Kyle Simpson: There we go. I provided you that utility as a hint, here, the When utility. So I'm just gonna copy and paste that in.
>> Kyle Simpson: Let's look at what the When utility does. When called the first time, it takes in a function and it's gonna use that function at the very end.
[00:02:48] So, that's basically gonna be our output function. So we're saying, when a predicate passes, that's the output that we wanna call. And then the next function that gets returned back, so it's expecting another input is the predicate function. That would be our test, like isShortEnough or isLongEnough. And then a third function that gets returned back expects the rest of the arguments, which in this case is gonna be the messages that we wanna pass.
[00:03:14] Once all three of those have been received, it passes the arguments into predicate. And if that returns true, it passes those same arguments into that original function, which would be output. So, we can define our printIf,
>> Kyle Simpson: As the when of the output function. Now, that is gonna return a function that's expecting a predicate.
[00:03:41] And we provide that predicate already by making the first call to the function here, we call it isShortEnough. And then that function that comes back is the one that's expecting the set of arguments, that second call there is the msg1. This pattern of providing arguments in one function call at a time.
[00:04:02] It has a name, and we're gonna go over that shortly later in the workshop. It can be refered to as carrying. And so we're gonna go over carrying, don't worry about that, if that part is still a little confusing. And now that we have a point free version of printIf.
>> Kyle Simpson: You could even go so far as to say is there some way to do a point free version of isShortEnough? And there are ways to do that but they would have used other more sophisticated patterns and functional programming that I haven't taught you about yet. So, the take away here is point free style is suppose to reduce the verbosity of your code, we have certainly eliminated some of the complexity.
[00:04:48] Made use of standard built-in libraries that as they may exist in a library for example. And those utilities give us the same result, but we didn't have to manually specify it. So remember how we set up at the beginning of the course, the goal here is to be more declarative?
[00:05:07] In a sense, this is the declarative form of what we had before, because we're saying, I don't really care how to do it, just negate the isShortEnough. Just make that part happen. Or make the predicate check happen for me using the when utility. I don't really care how that happens.
[00:05:24] You definitely are gonna wanna be careful about not going too far with this. Because this is where you can quickly start sliding down that curve that we talked about at the beginning of the course, where all of a sudden, you're looking at some line of code. And it's got 14 different adapter variations wrapped around it, and you're like I have no clue what this is even doing.
[00:05:47] So you can go too far with it. But it is a nice style to be aware of that can help make your, if you use it sparingly, it can help make your functional code a lot easier to read.