Check out a free preview of the full Functional-Light JavaScript, v3 course
The "Arguments Shape Adapters" 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 demonstrates how to change the arguments in a function to shape it, restricting an n-ary function into a unary function as an example.
Transcript from the "Arguments Shape Adapters" Lesson
[00:00:00]
>> Kyle Simpson: We can also adapt the shape of functions and this is where we're gonna really start to push your thinking around functional programming. Because we're gonna introduce a concept I'm sure many of you have heard this before. And maybe just didn't fully connect with what it meant. Have you heard the term before, higher order function?
[00:00:21]
Sometimes it's abbreviated HOF, H-O-F, okay, so what is a HOF? Sounds much more complicated than it actually is. That's the cool part, is that when we uncover these meanings, a lot of this stuff is actually pretty intuitive. Not always, but some of it anyway, so what is a higher order function?
[00:00:40]
Well, put simply, a higher-order function is a function that either receives, as its inputs. One or more functions, and/or returns one or more functions. That is if the value's going in, or the value's coming out our functions, then that thing itself is the higher order function. So a thing that doesn't receive functions, we would call it a single order function.
[00:01:11]
And if it receives functions or returns them, we call it a higher order function.
>> Kyle Simpson: So right on line 1, we see a utility that I've now called unary. You see that it receives a function denoted by the parameter fn. And it returns another function back, called 1, in this case.
[00:01:36]
>> Kyle Simpson: That's called a higher order function, the unary utility here is a higher order function. What is it doing? Well, it's kinda interesting because that one function that gets returned back, it only has one named parameter.
>> Kyle Simpson: You may or may not be aware that in JavaScript all functions are what we call variadic.
[00:01:58]
Meaning, it doesn't matter how many parameters are declared, you can pass as many arguments or as few as you want. You can define a function that expects three and you can pass four to it. You can find a function that expects three and only past one to it.
[00:02:11]
JavaScript is not gonna say [SOUND] you're doing something wrong, they're variadic functions. So if I were to take a function that might expect four and pass it, like for example, the f from Line 13, one that expects multiple ones. It's an n-ary function, but if I pass it to unary, what I get back, Line 17 g.
[00:02:35]
G is a new function, which has the name 1 that we see there on line 2. G, down on line 20, when we pass in four inputs, how many inputs actually make it to the underlying function?
>> Kyle Simpson: How many? Just one, because on line 2, we named just a single parameter arg.
[00:02:59]
So in other words, we took a function whose shape was to be n-ary. And we reduced its shape, we restricted its shape such that it is, what?
>> Kyle Simpson: It's unary, that's what the unary utility does, it's a higher order function that adapts the shape of a function to be unary.
[00:03:20]
We'll see used cases throughout the lecture and exercises where that kind of thing will be useful. But I want you to get more comfortable with the idea that a function of one shape can be adapted to have a different shape. That's critical, and that's a key takeaway from this higher order functions pattern.
[00:03:37]
Is that we can take any function and adapt its shape. And that's critical when we have two lego pieces that don't fit, and we need to sort of make ' fit. We gotta make an adapter, so that they fit, and that's what these utilities do. In fact, virtually all the utilities that you'll find in whichever functional programming library you choose to use.
[00:03:57]
RAM, Lodash whatever, whichever one you pick, all of the utilities are these kinds of higher order functions. Almost all of them do this kind of thing. They take a single function in, they do something, make a new function that does something different, has a different shape. So you're gonna see this kind of pattern all over the place, and what's critical is your takeaway at this moment.
[00:04:21]
Is that line 1 through 5 is not some sort of higher black magic kind of coding. It should feel very tractable, very sensible to you. Okay, I can see I take a function and make another function wrapped around it. And that's the key, because you as a functional programmer have to get comfortable with making your own higher order functions.
[00:04:46]
Not just using the result of a higher order function that's provided to you, but making your own. When you see that I have this piece and this piece and they don't fit. The functional programmer says, I can see what kind of adapter I need between them. And I make myself a utility to do that.
[00:05:05]
>> Kyle Simpson: And then I create the shape adjusted adapter, makes sense? So unary, g would be called the unary of f, line 18 we call h equals the binary of f. So on line 21, when you see h receiving four arguments. The underlying function, the f function, only receives two of them, we've made it the binary.
[00:05:31]
If you haven't dealt with higher order functions before, expect for that brain to feel a little, I don't know. But this is critical, it's going to be core and central to all of your future functional programming.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops