Do you consider implementing `Maybe#orElse()` method?
See original GitHub issueHi, thanks for the lib. Love it! ❤️
Do you find Maybe#orElse()
method useful and consider implementing it? I find it useful in order to gracefully avoid such constructions:
let maybeGTINs = getGTINsFromString(string);
if (maybeGTINs.isNone()) {
let maybeGTINs = just(['default']);
}
with something like this:
const maybeGTINs = getGTINsFromString(string).orElse(['default']);
Monet’s seem to have one, but it accepts a Maybe instance for some reason. I guess, the best implementation would be the one mentioned in this article: https://jrsinclair.com/articles/2016/marvellously-mysterious-javascript-maybe-monad/
Maybe.prototype.orElse = function(default) {
if (this.isNothing()) {
return Maybe.of(default);
}
return this;
};
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
When to use Optional.orElse() rather than Optional.orElseGet()
orElse() will always call the given function whether you want it or not, regardless of Optional.isPresent() value · orElseGet() will only call ...
Read more >Java 8 Optional isPresent(), OrElse() and get() Examples
You can see that the OrElse() method is much nicer than the combination of isPresent() and get(). It not only removes the check...
Read more >12 recipes for using the Optional class as it's meant to be used
So the rule here is to use orElse() when you have already preconstructed values and you don't use an expensive computed value.
Read more >Java Optional - orElse() vs orElseGet() - Baeldung
In this article, we learned the nuances between the Optional orElse() and OrElseGet() methods. We also discussed how such simple concepts can ......
Read more >26 Reasons Why Using Optional Correctly Is Not Optional
A typical scenario for using orElse(null) occurs when we have an Optional and we need to call a method that accepts null references...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Wow, that was fast! Thank you!
Great! I have some spare time, so if you would like me to propose a PR, just let me know 😉