Higher-Order Function
Function that takes other functions as arguments or returns a function as its result, such as map, filter and reduce.
A higher-order function accepts one or more functions as arguments, returns a function, or both. The standard examples work on collections: map applies a given function to every element of a list and returns the results; filter keeps only the elements for which a given test returns true; reduce combines the elements into a single value using a supplied operation. Sorting routines that take a custom comparison function are higher-order too. Languages that allow functions to be passed around like any other value are said to have first-class functions.
The idea comes from mathematics and logic, notably Alonzo Church's lambda calculus of the 1930s, in which functions take and return other functions. Lisp brought it into programming in the late 1950s, and it is central to functional programming languages such as ML and Haskell. Mainstream languages adopted it widely: JavaScript has had first-class functions from its start, and Python, C#, Java and C++ added lambda expressions or similar features. Functions returned by higher-order functions often rely on closures to remember the arguments they were created with.
Higher-order functions let programmers separate what to do from how to iterate. Instead of writing a fresh loop for every task, a program can combine small reusable operations into pipelines, reducing duplication in the spirit of the DRY principle. Callbacks in asynchronous programming are higher-order in the same sense. Overuse can harm readability, especially with deeply nested anonymous functions, and in some languages chains of map and filter allocate intermediate collections, although compilers and lazy evaluation often reduce that cost.
Key points
- Higher-order functions take functions as inputs, return functions as outputs or do both.
- Map, filter and reduce are standard higher-order functions for transforming and combining collections.
- The concept traces back to Alonzo Church's lambda calculus and entered programming through Lisp.
- They reduce repetitive loops by separating the operation to perform from the mechanics of iteration.
Related entries
Functional Programming
Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.
Closure
Function bundled with references to variables from the scope where it was created, letting it keep using them after that scope has finished.
Pure Function
Function that always returns the same output for the same inputs and has no side effects, such as modifying outside state or performing I/O.
Lisp
Family of programming languages originated by John McCarthy in 1958, built on symbolic expressions and long associated with AI research.
JavaScript
High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.
Recursion
Technique in which a function solves a problem by calling itself on smaller instances of the same problem until it reaches a simple base case.
Don't Repeat Yourself
Software development principle that every piece of knowledge in a system should have a single, authoritative representation rather than duplicates.
Referenced by
Other entries that discuss or point to Higher-Order Function.