A higher order function is a function that takes another function as argument. A well-known higher order function is map
, which performs an action on each element in a collection, e.g.:
-- Haskell
map (\x -> 2 * x) [1, 2, 3, 4, 5] -- => [2, 4, 6, 8, 10]
Higher order functions require that you can pass a function around as a first-class value, just as you can pass around numbers, strings, or other objects. This is either done by not making functions special at all (i.e. they are just ordinary objects with special syntax for creating them), or by providing special syntax to obtain a reference to a function. There might also be a syntax for anonymous functions (lambdas), but this is not strictly necessary in order to have higher order functions.
In object oriented languages, it's interesting to note that objects and closures are equivalent to each other. In fact, many languages represent first-class functions as an object with a method called apply()
or similar. Even before Java got lambdas, first-class functions could be implemented very verbosely by passing objects around, and anonymous classes are very similar to actual lambdas. The Command Pattern is essentially an encoding of first-class functions into an OOP context. So any OOP language can have higher order functions, but first class functions tend to involve an unreasonable amount of ceremony.
Languages with computed GO TOs are also equivalent to (actually, even more powerful than) first-class functions. While such languages tend to lack many type system guarantees and make writing obscure spaghetti code easy, they could be viewed as also providing first-class functions.
- C: has function pointers (i.e. first-class functions) and also supports higher-order functions. However, it does not have closures.
- Java: does OOP and therefore supports the Command Pattern. It also had anonymous classes for a while, which is a kind of lambda. With Java 8, it gained real lambdas and syntax to pass methods around as ordinary objects. Closures are limited.
- C++: has everything C has, plus the Command Pattern due to OOP. It gained various forms of lambdas in the C++11 revision. Closures are explicit, and limited by the memory management scheme.
- Python: Is an OOP language, but also has a number if Lisp influences (Lisp is the original functional programming language). Functions are just objects with fancy syntax. The function call operator is actually translated to a method call, which means that user-defined objects can also be used like a function. There is also a form of anonymous functions (lambda expressions), but they are ridiculously limited. All functions are closures, but they sometimes have to be explicit due to Python's scoping rules.
- JavaScript: Is the same story as with Python, except that it lacks operator overloading, and doesn't impose restrictions on lambdas. Closures work as expected.
- Perl: Supports both object oriented and functional paradigms. You can obtain a reference to a named function like
$code_ref = \&function_name
, and can call such references like $code_ref->(@arguments)
, which is equivalent to function_name(@arguments)
. Method calls can be performed by a run-time name, so that $object->method()
is the same as $name = "method"; $object->$name()
. Method resolution can be carried out to produce a code reference like $code_ref = $object->can("method")
. Anonymous functions are supported: $code_ref = sub { ... }
. Furthermore, the function call operator can be overloaded for objects. All functions are closures.
I can't comment on C#, PHP and the various BASIC dialects.