This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.
Transcript from the "Manual Composition" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Kyle: Our next topic, composition. And I know I said we'd try to stay away from fancy sounding words. So let me try to help explain what composition really means. Composition, in this sense is about taking the output of one function and putting it directly as the input to another function.
[00:00:25] So in other words, instead of calling one function and then calling another function, we're gonna call one function and its output is gonna become, at least part of the input for another function. And then the output of that could become part of the input for another function. That's what we mean by composition from a functionally programming perspective, at a very, very simple basic level.
[00:00:46] All right, so what would be an example of a program that might need some composition? Well if I had a function called sum that takes two variables and adds them together, that's a super useful function. You can all add that to your utility libraries. Now underscore, I don't even know if they have that yet.
[00:01:00] I invented the sum function and I invented the mult function, which multiplies, even more powerful. [LAUGH] I know, silly functions. I don't have very much room on these slides to show you codes so I have to use simple concepts here, okay? But if I wanted to perform an operation like five plus three times four, I'm going to have to compose these two functions in some way.
[00:01:24] I might compose them with state, I might save them into a variable like I'm doing with z, but now you can see that I'm doing sort of an impure thing. I have the side effect state. If on the other hand, I were to take the result of mult and pass it directly into sum, never saving it into some side variable, then I haven't created an impure action.
[00:01:49] I've simply composed two pure functions together. So right now this is an impure program because there is some side effect state that's happening. Is it a side effect in the sense that our program is gonna return different results? No, because we don't use that z variable, but there's still a side effect that's happening.
[00:02:07] Z is still getting changed. That is still a complexity that could create issues for your debugging later. So how could I reorganize this program so that I wasn't assigning to some variable? Well, I can simply pass in the result of mult, calling mult with three and four and passing its result directly into sum.
[00:02:25] In a functional programming sense, this is kind of a manual composition. So I didn't assign to some side variable state. I just took the end result and pumped it directly into this next program. So, the outer program here is pure. Even though technically, at a functional, I mean at a programmatic sense technically, our programming language has to temporarily store that variable somewhere.
[00:02:48] But from the perspective of our program, we've written a purer operation. Is everybody following that? That's why these concepts build upon each other. So composition is a way of reducing the amount of side effects you have. It is absolutely true that this is not some magic silver bullet that works everywhere, because you're not always in control of the design of your function signatures.
[00:03:10] And if our function signature happened to not work in such a nice way where we could pass in a value like that or if one of our functions needed to return two values but the thing we need to pass it to only needed one of those, it gets a little more complicated.
[00:03:25] Granted, that is absolutely true. This is why we call this functional-light programming. We're looking for ways to develop those instincts and to whatever extent that you can develop that instinct it may guide your design decisions for the way you write your functions. You may write your functions assuming that somebody may wanna compose that with a different function.
[00:03:46] That is not only the argument signature, but also the return value signature. So if we wanted to create a pure wrapper around this to do this composition for us, we could do it manually. We could call it multAndSum and pass in all of the values, x, y, and z.
[00:04:05] And simply call mult and then pass its value into sum, that's a manual composition. We can create ourselves a little utility as a manual composition.