Skip to content

Unwrapping fold and rewrapping as result #143

Description

@grisumbras

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).

https://godbolt.org/z/74MsK345d

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions