In mathematics, a functor is a type of mapping between categories which is applied in category theory. Functors can be thought of as homomorphisms between categories. In the category of small categories, functors can be thought of more generally as morphisms.
Functors were first considered in algebraic topology, where algebraic objects (like the fundamental group) are associated to topological spaces, and algebraic homomorphisms are associated to continuous maps. Nowadays, functors are used throughout modern mathematics to relate various categories. Thus, functors are generally applicable in areas within mathematics that category theory can make an abstraction of.
The word functor was borrowed by mathematicians from the philosopher Rudolf Carnap, who used the term in a linguistic context: see function word.
Let C and D be categories. A functor F from C to D is a mapping that
In the context of the C++ programming language, functional
refers to a header file that is part of the C++ Standard Library and provides a number of predefined class templates for function objects, including arithmetic operations, comparisons, and logical operations. Instances of these class templates are C++ classes that define a function call operator, and the instances of these classes can be called as if they were functions. It is possible to perform very sophisticated operations without actually writing a new function object, simply by combining predefined function objects and function object adaptors.
The class template std::function
provided by C++11 is a general-purpose polymorphic function wrapper. Instances of std::function
can store, copy, and invoke any callable target—functions, lambda expressions (expressions defining anonymous functions), bind expressions (instances of function adapters that transform functions to other functions of smaller arity by providing values for some of the arguments), or other function objects.
In many programming languages, map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results in the same order. It is often called apply-to-all when considered in functional form.
The concept of a map is not limited to lists: it works for sequential containers, tree-like containers, or even abstract containers such as futures and promises.
Suppose we have a list of integers [1, 2, 3, 4, 5]
and would like to calculate the square of each integer. To do this, we first define a function to square
a single number (shown here in Haskell):
Afterwards we may call
which yields [1, 4, 9, 16, 25]
, demonstrating that map
has gone through the entire list and applied the function square
to each element. The map
is provided as part of the Haskell's base prelude (i.e. "standard library") and is implemented as:
In Haskell, the polymorphic function map :: (a -> b) -> [a] -> [b]
is generalized to a polytypic function fmap :: Functor f => (a -> b) -> f a -> f b
, which applies to any type belonging the Functor
type class.