Higher-Order Function
A function that takes another function as an argument, returns one, or both.
Higher-Order Function is a function that treats functions as ordinary data: it accepts one or more as arguments, returns one as a result, or both. map, filter, and reduce are the canonical examples — each takes a function describing what to do per element and handles the looping itself, separating the traversal (fixed, boring, easy to get wrong by hand) from the per-element logic (the part that actually varies).
The requirement underneath is that functions be first-class values in the language — assignable to variables, passable as arguments, storable in data structures — which most languages have had for decades but which older or more restrictive ones (early Java, C without function pointers) made awkward. A callback, an event handler, a comparator passed to a sort function, and a middleware chain are all higher-order functions in disguise: something is accepting a function and deciding when to call it.
Higher-order functions are the mechanism Currying and function composition run on — composing f and g into a single function is itself a higher-order operation, since it takes two functions and returns a third. They're also where a Closure earns its keep: a function returned from another function typically needs to remember variables from the enclosing scope, and a closure is exactly that memory.
The risk is indirection outrunning its benefit — a codebase where every operation is expressed as a composition of tiny higher-order combinators can become harder to read than the loop it replaced, because a reader has to mentally execute the composition instead of just reading top to bottom. The map/filter/reduce trio earns its place by matching a shape readers already recognize; a bespoke higher-order abstraction has to earn that recognition the hard way, one reader at a time.
See also4
Pure Function
A function whose output depends only on its input and which causes no observable effect outside itself.
Computation & Algorithms13 connections
Recursion
A function defined in terms of itself, solving a problem by solving smaller instances of the same problem.
Computation & Algorithms10 connections
Iterator
An object that hands out the elements of a collection or sequence one at a time on request, separating how a sequence is produced from how it's consumed, which lets the same loop walk arrays, trees, files and infinite streams.
Computation & Algorithms7 connections
Middleware
Functions that sit between an incoming web request and the code that answers it, each able to inspect, change, or end the request, chained in an order that quietly determines how the whole application behaves.
Web Platform8 connections
Linked from6
- ClosureComputation & Algorithms
A function bundled with the variables from its enclosing scope at the point it was created.
- CurryingComputation & Algorithms
Transforming a function of several arguments into a chain of one-argument functions, each returning the next.
- GenericsComputation & Algorithms
Writing code once over a type variable so it works for any type, how languages implement it by erasure or by copying, and what constraints on type parameters buy.
- IteratorComputation & Algorithms
An object that hands out the elements of a collection or sequence one at a time on request, separating how a sequence is produced from how it's consumed, which lets the same loop walk arrays, trees, files and infinite streams.
- MiddlewareWeb Platform
Functions that sit between an incoming web request and the code that answers it, each able to inspect, change, or end the request, chained in an order that quietly determines how the whole application behaves.
- Pure FunctionComputation & Algorithms
A function whose output depends only on its input and which causes no observable effect outside itself.