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

Shortcut for flatMap on possibly falsy data (flatMapFromFalsy) #217

Open
tvedtorama opened this issue Aug 16, 2019 · 1 comment
Open

Shortcut for flatMap on possibly falsy data (flatMapFromFalsy) #217

tvedtorama opened this issue Aug 16, 2019 · 1 comment

Comments

@tvedtorama
Copy link

I find myself writing this all the time:

maybe.flatMap(x => Maybe.fromFalsy(x.value)).

It would be very useful to have an

maybe.flatMapFromFalsy(x => x.value)

or

maybe.flatMap(x => x.value, Maybe.fromFalsy)
// Which can also be used with other constructors:
maybe.flatMap(x => x.value, Maybe.fromNull)

Does this make sense, or am I perhaps using the library a bit wrong?

@ulfryk
Copy link
Member

ulfryk commented Aug 16, 2019

@tvedtorama - your request is for sure a thing. I also find myself composing flat map with fromNull

Without need to access nested properties solution would be using the constructor as a passed lambda:

maybe.flatMap(Maybe.fromFalsy)

but for mentioned above case there are no tools in monet. You can always use compose from any other functional library:

maybe.flatMap(compose(Maybe.fromFalsy, x => x.value))

But as this seems to be quite a common use case, I'd think about how it can be enhanced in a nicest way :)

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

No branches or pull requests

2 participants