Higher-order functions are function that can take another function as an input.
" this feature is available in a number of modern languages like JavaScript, Pascal, Swift, and a whole bunch of other ones, but it's not universal. It's not something that's available in all programming languages "
Some Wikipedia
In mathematics and computer science, a higher-order function is a function that does at least one of the following:
- takes one or more functions as arguments (i.e. procedural parameters),
- returns a function as its result.
In mathematics and computer science, a higher-order function is a function that does at least one of the following:
takes one or more functions as arguments (i.e. procedural parameters), returns a function as its result.
Code example
JavaScript
function add(a,b) { return a+b }
function multiply(a,b) { return a*b }
function calculator(a,b,operator){ return operator(a,b) }
C++
#include <iostream>
auto twice = [](auto f, int v)
{
return f(f(v));
};
auto f = [](int i)
{
return i + 3;
};
int main()
{
std::cout << twice(f, 7) << std::endl;
}
Python
>>> def twice(f):
... def result(a):
... return f(f(a))
... return result
>>> plusthree = lambda x: x + 3
>>> g = twice(plusthree)
>>> g(7)