Composition involves taking the output from one function and directly inputting it into another function. Kyle demonstrates how composition can be created manually by calling a function within the parameter list of another function. He also shows how to create a manual composition by writing a separate function.
- 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. So in other words, instead of calling one function and then calling another function, we're going to call one function and its output is going to 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 functional programming perspective. At a very very simple basic level. Alright, so, what would be an example of a program that might need some composition? What if I had a function called sum that takes two variables and adds them together that's a super use function. You can all add that to your utility libraries. Now Underscore I don't even know if they have that yet. I invented the sum function and I invented the mult function which multiplies. Even more powerful.
Okay. I know, silly functions. I don't have very much room on these slides to show you code so I have to use simple concepts here. Okay? But if I wanted to perform an operation like 5+3x4 I'm going to have to compose these two functions in some way. 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 this 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.
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 a sense that our program is going to return different results? No, because we don't use that z variable. But there's still a side effect that's happening. Z is still getting changed. It 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 could simply pass in the result of mult calling mult with three and four and passing its result directly in to sum.
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 programmatic sense technically our programming language has to temporarily store that variable somewhere. But from the perspective of our program we've written a pure 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 we have. Now 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. And if our function signature happened to not work in such a nice way where we could pass an evalu 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. Granted. That is absolutely true.
This is why we call this functional-light program. We're looking for ways to develop those instincts. And to whatever extent 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 want to compose that with a different function. 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 mult and sum, pass in all of the values x, y, and z and simply call mult and then pass its value into sum. That's a manual composition. We could create ourselves a little utility as a manual composition.
This course was created by Frontend Masters. It was originally released on 03/08/2016. We're pleased to host this training in our library.
- Pure functions
- Manual composition
- Composition utility
- List operations