Higher order functions - step by step - part III
Return functions
Let’s imagine we want to filter
the people
in different ways. Not only the ones who canDrive
. Let’s suppose we also want to filter
the people
who are named “Jane”.
We are passing the people
to each one of those calls. If we were only filtering people
, that may seem redundant. We could define a function that only filters on the people
.
But that only works in this context because it is tied directly to people
. We would like to do that with any collection.
The result of the function filterCollection
is another function with one parameter less. That is called partial application. Which means that we are predefining arguments.
But that is only valid for the filter
function and only for the first argument. We would like to do that with any function and any amount of arguments.
Now we can only bind
the first arguments to a function. We would like to bind
any combination of arguments to a function. But this time, intead of trying to generalise bind
for that purpose, we are going to cycle
the arguments of a function so we can then bind
the ones we prefer.
The last thing we would like to do is to compose
a single function from other functions, having the output from one function going to the input of the next. With that, we can reproduce the behaviour of the first piece of code in the starting point.
There we have code at a much higher level of abstraction. In contrast with the code at the starting point, this one enables us to see what the program is doing in human terms: given people
treats only drivers
, gets their names
and prints them
.
Each function does only one thing and does it well. You can extend their functionality by combining them, not modifying them. Because they are functions, as long as you keep the signature, you can change their implementations and the users will not notice. All of that makes them very loosely coupled and as a result they are extremely easy to test as units.
You can feel the similarity with a SOLID-conforming object-oriented system and their very nice composable properties. I like to see [higher-order functions][hof] as a way to create a kind of lego system, a system with very flexible little pieces that make easy to create big and very expressive worlds.