What is function composition?

Published: 02 April 2019
on channel: Eric Normand
288
4

Function composition is taking the return value of one function and passing it as an argument to another function. It's common enough that functional programmers have turned it into its own operation. In this episode, we go deep into why it's important and how you can use it and write it yourself.

Transcript

What is function composition? By the end of this episode, you will know what function composition is and why it's useful. My name is Eric Normand, and I help people thrive with functional programming.

This is an important topic because it's something we do all the time. We do function composition in both functional and non-functional styles of programming.

Functional programmers and mathematicians have named it, they've named it functional composition, and they've turned it into a higher order function. It's something that we tend to talk about probably more than you would in a non-functional language.

What is it? It's pretty simple. It's when you take a function and you call it, and then you take the return value of that function and you pass it to a second function as an argument. You're chaining the function calls.

Usually, in math or as examples, we use function f and g. You call function g, you get the result. You take that result and you pass it to function f. Of course, you get the result of function f.

There's a special case because sometimes you'll need some other arguments, like f will take it as the second argument. It will take the result of g as the second argument.

There's a special case where you take that return value of g and you pass it as the first and only argument to f.

That's a special case because it's very simple and easy to reproduce in your code that you can just do f, open paren...I'm using JavaScript syntax, f, open paren, g, open paren. You put the arguments to g, and then you close the paren and you close the paren of f.

Now, the g is the...You don't even need to name that return value, you don't need to save it into a variable. You pass it directly to f.

If you take this special case -- because it's so regular -- you can turn it into a higher-order function. That is, a function that takes f and g and returns a new function that does that same thing, that does the f open paren g.

Mathematicians usually use the dot as the operator for a function composition when they want to talk about it in a paper or something. They'll write, "f.g," to mean f composed with g. This gets confusing, and it's one of the reasons why function composition is thought of as difficult.

The confusing part is that the g is called before f, even though it comes to the right of f. If I do f.g, the g is called first, and then the f is called on the result of g. It looks backwards. It looks like, "Call f," and then, "Call g," but the g is called first. That makes it confusing.

The reason it is that way is, like I said, it's like doing f open paren g, open paren, put the args of g, and then close the parens. They're in the same order as if you were to call it directly, but for clarity, you might want to move the g out of there, because it's inside out.

The arguments get evaluated first before the function gets called on those arguments. If you move the g above there, and you called it g result equals g, and then you called it with its arguments, and then after that, you did f open paren g result...

I'm trying to talk through code, but the idea is that it's in the same order as something you should be familiar with. It's just when you look at it on it's own and that's not explained to you, it looks backwards.

Why do we talk about this? Why do we want to turn this into a higher-order function?

First of all, it's very common to do this. It's common to call a function and pass that argument to the next one. Even though it's less common than the general case, the special case of the single argument is still really common.

Let's say you wanted to double a number and then square it. You could double it, so you call the double function, then you take the result of that and you pass it to the square function, which then returns the value.

You could say, "This is a function composition," and you can call compose with the two functions. That gives you a new function, which is the double square.

What it does is it reduces boilerplate. By reifying it...I have a whole episode on reification if you want to know what that is. I also have a whole episode planned for higher-order functions, if you want to know more about that.


On this page of the site you can watch the video online What is function composition? with a duration of hours minute second in good quality, which was uploaded by the user Eric Normand 02 April 2019, share the link with friends and acquaintances, this video has already been watched 288 times on youtube and it was liked by 4 viewers. Enjoy your viewing!