concept transform in category c++

This is an excerpt from Manning's book Functional Programming in C++.
transform
—What the code does
A class template
F
is a functor if it has atransform
(ormap
) function defined on it (see figure 10.1). Thetransform
function takes an instancef
of a typeF<T1>
and a functiont: T1 → T2
, and it returns a value of typeF<T2>
. This function can have multiple forms, so we’ll use the pipe notation from chapter 7 for clarity.
Figure 10.2 The main rule the
transform
function needs to obey is that composing two transforms—one that lifts functionf
and another that lifts functiong
—needs to have the same effect as having a single transformation with the composition off
andg
.![]()
This looks much like the
std::transform
algorithm andview::transform
from the ranges library. That’s not accidental: generic collections from the STL and ranges are functors. They’re all wrapper types that have a well-behavedtransform
function defined on them. It’s important to note that the other direction doesn’t hold: not all functors are collections or ranges.
You can—and this is a more common way to define monads. You can say that a monad
M
is a wrapper type that has a constructor (a function that constructs an instance ofM<T>
from a value of typeT
) and anmbind
function (it’s usually called justbind
, but we’ll use this name so it doesn’t get confused withstd::bind
), which is a composition oftransform
andjoin
:construct : T → M<T> mbind : (M<T1>, T1 → M<T2>) → M<T2>It’s easy to show that all monads are functors. It’s trivial to implement
transform
by usingmbind
andconstruct
.