The Vector Monad in C++, Really Without the Ugly Stuff
Barry Revzin
101

You can also rely on range-v3 to get the job done (and done lazily).

The views in range-v3 already implement the concept of Monad through view::join, which flattens a range of range into a range. Combined with view::transform, you get the implementation of bind (or “and_then”): just call view::transform piped to view::join.

std::vector<int> xs = {1, 2, 3};
std::vector<int> ys = {3, 4, 5};

cartesian_product(
 xs | view::transform(f1),
 ys | view::transform(f1))
 | view::transform(tupled_args(f2))
 | view::join
 | view::transform(f3);

Like what you read? Give Quentin Duval a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.