In my previous post I tried to explain tail call optimization.
If you read it to the end you saw that map function that took a collection and a function as its arguments.
This already was a higher order function. The definition is straight forward:
A higher order function takes a function as an argument or returns a function as its result, or both.
So lets say you work in a big company and you have to calculate the bonuses for the employees. Of course, not everyone gets the same bonus, but you’re too lazy to pass the specific multiplier at every calculation. So you create seperate functions.
In this example the C-level department members get a bonus of 1.5 of their annual salary. While the salaries may differ, the multiplier stays the same.
defmodule Salary do
You may be wondering, why our c_level_bonuses function still knows about the percentage since we haven’t explicitly passed that value.
That’s because the function has knowledge about the environment it was created in - it’s a closure.
If you want to see an example for a function that takes another function as an argument, head to my previous post and take a look at the map function.
And in case you feel adventurous, try to find a use case to write a function that takes and returns a function =)