Dobiasd / FunctionalPlus

Functional Programming Library for C++. Write concise and readable C++ code.
http://www.editgym.com/fplus-api-search/
Boost Software License 1.0
2.07k stars 168 forks source link

Methods directly on containers (e.g `maybe`) #288

Closed tom91136 closed 7 months ago

tom91136 commented 7 months ago

I was wondering would adding a few more methods to some of the containers would help with productivity. For example:

    auto o = fplus::maybe<int>(1);
    auto p = fwd::apply(
        o,
        fwd::and_then_maybe([](auto x) { return x > 0 ? fplus::just(1) : fplus::nothing<int>(); }),
        fwd::lift_maybe([](auto x) { return x * 2; }),
        fwd::just_with_default(42)
    );

    auto p2 = o
            .and_then([](auto x) { return x > 0 ? fplus::just(1) : fplus::nothing<int>(); })
            .lift([](auto x) { return x * 2; })
            .get_with_default(42);

That way:

I'm happy to open a PR if we think this is a good idea.

Dobiasd commented 7 months ago

Oh, that looks like a splendid idea to me in terms of code readability. :heart:

Yeah, we should keep the free functions (to not break old client code). And, I agree, letting the free functions forward to the member functions (instead of the other way around) is the way to go, exactly because of the reason you mentioned. :+1:

I'd be happy to receive your PR. :slightly_smiling_face: