An introduction to higher-order functions
In essence, a higher-order function is any function that either takes a function as the input or returns a function as the output. Recall from the previous chapter that both of these things are made possible through the support for functions as “first-class citizens.” Although it’s perhaps uncommon to call them “higher-order functions,” many programming languages do support these functions out of the box. For example, in Java and Python, the map
, filter
, and reduce
functions are all examples of higher-order functions.
Let’s create a simple example in Go. We’ll have a function, A
, that returns hello,
and a function, B
, that takes A
as an input parameter. This is a higher-order function, as the A
function is used as input to B
:
func A() string { return "hello" } func B(a A) string { return A() + " world" }...