The Functor
concept represents types that can be mapped over.
Intuitively, a Functor is some kind of box that can hold generic data and map a function over this data to create a new, transformed box. Because we are only interested in mapping a function over the contents of a black box, the only real requirement for being a functor is to provide a function which can do the mapping, along with a couple of guarantees that the mapping is well-behaved. Those requirements are made precise in the laws below. The pattern captured by Functor
is very general, which makes it widely useful. A lot of objects can be made Functor
s in one way or another, the most obvious example being sequences with the usual mapping of the function on each element. While this documentation will not go into much more details about the nature of functors, the Typeclassopedia is a nice Haskell-oriented resource for such information.
Functors are parametric data types which are parameterized over the data type of the objects they contain. Like everywhere else in Hana, this parametricity is only at the documentation level and it is not enforced.
In this library, the mapping function is called transform
after the std::transform
algorithm, but other programming languages have given it different names (usually map
).
transform
transform
is specified, adjust_if
is defined analogously to adjust_if
adjust_if
is specified, transform
is defined analogously to Let xs
be a Functor with tag F(A)
, \( f : A \to B \) and \( g : B \to C \). The following laws must be satisfied:
The first line says that mapping the identity function should not do anything, which precludes the functor from doing something nasty behind the scenes. The second line states that mapping the composition of two functions is the same as mapping the first function, and then the second on the result. While the usual functor laws are usually restricted to the above, this library includes other convenience methods and they should satisfy the following equations. Let xs
be a Functor with tag F(A)
, \( f : A \to A \), \( \mathrm{pred} : A \to \mathrm{Bool} \) for some Logical
Bool
, and oldval
, newval
, value
objects of tag A
. Then,
The default definition of the methods will satisfy these equations.
hana::lazy
, hana::optional
, hana::tuple
A mapping between two functors which also preserves the functor laws is called a natural transformation (the term comes from category theory). A natural transformation is a function f
from a functor F
to a functor G
such that for every other function g
with an appropriate signature and for every object xs
of tag F(X)
,
There are several examples of such transformations, like to<tuple_tag>
when applied to an optional value. Indeed, for any function g
and hana::optional
opt
,
Of course, natural transformations are not limited to the to<...>
functions. However, note that any conversion function between Functors should be natural for the behavior of the conversion to be intuitive.
Variables | |
constexpr auto | boost::hana::adjust |
Apply a function on all the elements of a structure that compare equal to some value. More... | |
constexpr auto | boost::hana::adjust_if |
Apply a function on all the elements of a structure satisfying a predicate.Given a Functor, a predicate pred and a function f , adjust_if will adjust the elements of the Functor that satisfy the predicate with the function f . In other words, adjust_if will return a new Functor equal to the original one, except that the elements satisfying the predicate will be transformed with the given function. Elements for which the predicate is not satisfied are left untouched, and they are kept as-is in the resulting Functor. More... | |
constexpr auto | boost::hana::fill |
Replace all the elements of a structure with a fixed value. More... | |
constexpr auto | boost::hana::replace |
Replace all the elements of a structure that compare equal to some value with some new fixed value. More... | |
constexpr auto | boost::hana::replace_if |
Replace all the elements of a structure satisfying a predicate with a fixed value. More... | |
constexpr auto | boost::hana::transform |
Map a function over a Functor . More... | |
constexpr auto boost::hana::adjust |
#include <boost/hana/fwd/adjust.hpp>
Apply a function on all the elements of a structure that compare equal to some value.
Given F
a Functor and U
a type that can be compared with T
's, the signature is \( \mathtt{adjust} : F(T) \times U \times (T \to T) \to F(T) \)
xs | The structure to adjust with f . |
value | An object that is compared with each element x of the structure. Elements of the structure that compare equal to value are adjusted with the f function. |
f | A function called as f(x) on the element(s) of the structure that compare equal to value . |
constexpr auto boost::hana::adjust_if |
#include <boost/hana/fwd/adjust_if.hpp>
Apply a function on all the elements of a structure satisfying a predicate.Given a Functor, a predicate pred
and a function f
, adjust_if
will adjust the elements of the Functor that satisfy the predicate with the function f
. In other words, adjust_if
will return a new Functor equal to the original one, except that the elements satisfying the predicate will be transformed with the given function. Elements for which the predicate is not satisfied are left untouched, and they are kept as-is in the resulting Functor.
Given a Functor
F
and a Logical
Bool
, the signature is \( \mathtt{adjust\_if} : F(T) \times (T \to Bool) \times (T \to T) \to F(T) \)
xs | The structure to adjust with f . |
pred | A function called as pred(x) for each element of the Functor, and returning whether f should be applied on that element. |
f | A function called as f(x) on the element(s) of the Functor that satisfy the predicate. |
constexpr auto boost::hana::fill |
#include <boost/hana/fwd/fill.hpp>
Replace all the elements of a structure with a fixed value.
Given F
a Functor, the signature is \( \mathtt{fill} : F(T) \times U \to F(U) \)
xs | The structure to fill with a value . |
value | A value by which every element x of the structure is replaced, unconditionally. |
constexpr auto boost::hana::replace |
#include <boost/hana/fwd/replace.hpp>
Replace all the elements of a structure that compare equal to some value
with some new fixed value.
Given F
a Functor and U
a type that can be compared with T
, the signature is \( \mathtt{replace} : F(T) \times U \times T \to F(T) \)
xs | The structure to replace elements of. |
oldval | An object compared with each element of the structure. Elements of the structure that compare equal to oldval are replaced by newval in the new structure. |
newval | A value by which every element x of the structure that compares equal to oldval is replaced. |
constexpr auto boost::hana::replace_if |
#include <boost/hana/fwd/replace_if.hpp>
Replace all the elements of a structure satisfying a predicate
with a fixed value.
Given F
a Functor and Bool
a Logical, the signature is \( \mathtt{replace\_if} : F(T) \times (T \to Bool) \times T \to F(T) \)
xs | The structure to replace elements of. |
predicate | A function called as predicate(x) for element(s) x of the structure and returning a Logical representing whether x should be replaced by value . |
value | A value by which every element x of the structure for which predicate returns a true-valued Logical is replaced. |
constexpr auto boost::hana::transform |
#include <boost/hana/fwd/transform.hpp>
Map a function over a Functor
.
Given F
a Functor, the signature is \( \mathtt{transform} : F(T) \times (T \to U) \to F(U) \)
xs | The structure to map f over. |
f | A function called as f(x) on element(s) x of the structure, and returning a new value to replace x in the structure. |