> For the complete documentation index, see [llms.txt](https://hanfak.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hanfak.gitbook.io/workspace/general-paradigms/programming-types/functional-programming/higher-order-functions.md).

# Higher Order Functions

* Definition
  * when a function accepts another function as its argument, or it yields another function as its return value
  * or both
* Examples
  * map, filter

## Function composition

* To compose two functions means to arrange them so that the result of one function is applied directly as the input of the other function.
* it is useful to consider two composed functions as a third function in its own right.
* It is a form of chaining
* Say you have a function f that takes a value x as its argument and returns a value y :
  * f ( x ) = y
  * and you have another function g that takes y as its argument and returns z
  * g ( y ) = z
  * clearly, then, you can apply g to the output of f like this :
  * g ( f ( x )) = z
  * This implies, therefore, that there is a third function h that maps x directly to z :
  * h ( x ) = z
* Examples
  * unix pipes ie `ls -al | grep blah`
  * map(x -> ...).filter(x -> ...)
  * builder pattern
