concept transform in category c++

appears as: trsform, transform, trsform
Functional Programming in 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 a transform (or map) function defined on it (see figure 10.1). The transform function takes an instance f of a type F<T1> and a function t: T1 → T2, and it returns a value of type F<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 function f and another that lifts function g—needs to have the same effect as having a single transformation with the composition of f and g.

    c10_02.png

    This looks much like the std::transform algorithm and view::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-behaved transform 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 of M<T> from a value of type T) and an mbind function (it’s usually called just bind, but we’ll use this name so it doesn’t get confused with std::bind), which is a composition of transform and join:

    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 using mbind and construct.

    sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest