Skip to content

An or function which returns the first non empty optional #28

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

Closed
cbefus opened this issue Mar 6, 2019 · 1 comment
Closed

An or function which returns the first non empty optional #28

cbefus opened this issue Mar 6, 2019 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@cbefus
Copy link
Collaborator

cbefus commented Mar 6, 2019

something like:

return Optional.empty() \
     .or(Optional.empty()) \
     .or(Optional.of(3)) \
     .or(Optional.of(4))

would return Optional.of(3) where as:

return Optional.empty() \
     .or(Optional.empty()) \
     .or(Optional.empty())

would return Optional.empty()

It would also be nice if we could pass suppliers, such that we only execute a supplier if the result of the previous suppliers was empty.

@cbefus cbefus added the enhancement New feature or request label Mar 6, 2019
@cbefus cbefus self-assigned this Mar 6, 2019
@dpassen
Copy link
Collaborator

dpassen commented Mar 6, 2019

For documentation's sake, the solution here is Python's built-in or.

The first example can be done as follows:

return Optional.empty() or \
       Optional.empty() or \
       Optional.of(3) or \
       Optional.of(4)

and the second:

return Optional.empty() or \
       Optional.empty() or \
       Optional.empty()

@dpassen dpassen closed this as completed Mar 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants