Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monadic (fluent) interface for maybe #289

Merged
merged 2 commits into from
Dec 26, 2023

Conversation

tom91136
Copy link
Contributor

This PR implements #288.

I've added new checks directly below the existing maybe tests to make sure it compiles.
An extra test on chaining the fluent interface is also included, we can now do:

maybe<int> m(42);
m
  .just_if([](auto x) { return x > 0; })
  .and_then([](auto x) { return x % 2 == 0 ? just<std::string>("a") : nothing<std::string>(); })
  .lift([](auto x) { return just(x); })
  .join()
  .lift_def(nothing<std::string>(), [](auto x) { return just(x + "b"); })
  .get_with_default("c"); // == "ab"

I've made the following additions to the API:

  • A just_if method (e.g filterM for our maybe functor) which I believe is missing.
  • Moved is_maybe from internal to top-level as we need this for join, and I also think this is a very useful API to expose.

Add just_if for filtering maybe
@Dobiasd
Copy link
Owner

Dobiasd commented Dec 26, 2023

That looks splendid! Working with Maybe types is much more convenient that way. 😍

The only potential downside of the member functions over the free functions might be the auto return type on some. However, adding an explicit return type could be quite complex with the derivation based on the passed F, etc. So let's keep it that way (with auto). In case some users don't like it because the auto-completion in their IDE does not play too well with this, they can still use the free functions (which have explicit return types). ✅

@Dobiasd
Copy link
Owner

Dobiasd commented Dec 26, 2023

A just_if method (e.g filterM for our maybe functor) which I believe is missing.

That's a very nice addition! 🚀

@Dobiasd
Copy link
Owner

Dobiasd commented Dec 26, 2023

Moved is_maybe from internal to top-level as we need this for join, and I also think this is a very useful API to expose.

Since we don't expose such helpers in other parts of the library, I'm not yet sure about exposing is_maybe to the fplus namespace here. .join (and .and_then) could also use is_maybe if is_maybe would still be defined in internal (at the same location in the file, i.e., above the class definition). 🤔

@tom91136
Copy link
Contributor Author

Sure, I've reverted that part.

@Dobiasd Dobiasd merged commit 2a94503 into Dobiasd:master Dec 26, 2023
19 checks passed
@Dobiasd
Copy link
Owner

Dobiasd commented Dec 26, 2023

Cool. 👍

And, thanks a lot for this awesome contribution! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants