logo

Higher-Order Functions in Programming 📂Programing

Higher-Order Functions in Programming

Definition

In programming, when a function is a first-class object, the function that takes or returns a function is called a Higher-order Function. Conversely, a function that is not a higher-order function is called a First-order Function.


  • The functions referred to in this definition are not the functions discussed in mathematics, but functions in computer science, that is, functions that might have side effects.

Explanation

Mathematics’ Functionals

Occasionally, there’s mention that the context of functionals in mathematics seems similar, but no one goes into detail, so I’ll explain. It’s not important, so you can skip this if you’re confused.

In mathematics, the function of a vector space $X$ is called a functional, and among them, the collection of continuous and linear functionals, the dual space, is expressed as $X^{\ast}$. Thinking of a dual once more in $X^{\ast \ast}$, its domain is the collection of continuous linear functions $\lambda: X \to \mathbb{C}$ in $X^{\ast}$. This can be seen as a higher-order function that takes a function as an argument in programming.

But again, this is only mentioned in case someone might be curious, not because it’s important. It’s okay not to know it.

Map and Fold

Don’t read thoroughly, just skim: If you ask a coder who prefers functional programming what the essence of functional programming is, their answers may vary. However, if you ask a coder with no interest in functional programming what the most useful part of it is, most likely, they’ll mention Map and Reduce. A map is a (2) function that takes a (1) function and a collection, applying the function to each element of the collection and returning a collection of the results, and a fold is a (2) function that takes a (1) function and a collection, applying the function to the collection itself and returning the result.

The significance and convenience of map and fold (reduce) aside, what matters now is not exactly what they are, but that they are explained as functions of functions. To put it briefly, “map and reduce are higher-order functions,” and “the advantage of functional programming is that it allows you to use map or reduce” can become an intricate sentence that is hard for non-majors to understand and sounds knowledgeable.