-
Notifications
You must be signed in to change notification settings - Fork 93
Description
A common operation with "monadic" wrappers is to take a collection of wrapped values and repackage it as a wrapped collection of values. In other words vector<result<T>> -> result<vector<T>>. In C++ this exact operation is too opinionated (we don't usually actually want to create a new container), so instead I propose a special "fold". Or actually several versions of it.
All three would have the signiture R (Rng, Z, F).
The first version would require Rng to be a range of result<T, E> and F have the signiture auto (Z&, T&&) -> Z. R would be result<Z, E>.
The second version would require Rng to be a range of T, and F have the signiture auto (Z&, T&&) -> result<Z, E>. R would be result<Z, E>.
The third version would require Rng to be a range of result<T, E>, and F have the signiture auto (Z&, T&&) -> result<Z, E>. R would be result<Z, E>. The second version can be viewed as a degenerated version of the third version.
Below is an example of me my implementation of version 1 to implement a custom conversion from to boost::json::value to a container type. Container conversion provided by Boost.JSON have a similar structure. Possibly, I would actually prefer version 2 here (my source range isn't a range of results).