-
Notifications
You must be signed in to change notification settings - Fork 169
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 Seq<T> onEmptySwitch(Supplier<? extends Stream<? extends T>>) #179
Comments
Sample implementation: default Seq<T> onEmptySwitch(Supplier<? extends Stream<T>> supplier) {
Supplier<? extends Stream<T>> lazy = () -> {
Iterator<T> it = iterator();
return it.hasNext() ? seq(it) : supplier.get();
};
return of(lazy).flatMap(Supplier::get);
} As for your question about overloads, maybe different names like: |
Thank you for your suggestion, @tlinkowski. Hmm, I'm not sure it is correct, though. Why are you flatmapping your "lazy" stream with But still, the fact that we'll need these wonky overloads to be sure we can get around type erasure, I'm still a bit hesitant to add this at all. |
No, no - this default Seq<T> onEmptySwitch(Supplier<? extends Stream<T>> supplier) {
return lazy(() -> {
Iterator<T> it = iterator();
return it.hasNext() ? seq(it) : supplier.get();
});
} |
Oh yeah, my bad. I get it. I was confused about the casing of |
As suggested by @johnmcclean-aol (#177 (comment)), an
onEmptySwitch(Supplier<? extends Stream<? extends T>>)
method might be useful in some situations.Criticism:
onEmptyXXX()
names were inspired byOptional.orElseXXX()
. We should think about the termswitch
, or if there is any more suitable term for this actiononEmptySwitch(Supplier<E>)
method with different bounds for<E>
. What would be the best upper bound?Seq
?Stream
?Iterable
?The text was updated successfully, but these errors were encountered: