Open lukaseder opened 8 years ago
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: onEmptyGetStream
, onEmptyGetSeq
, and onEmptyGetIterable
?
Thank you for your suggestion, @tlinkowski. Hmm, I'm not sure it is correct, though. Why are you flatmapping your "lazy" stream with Supplier
get? If the this
stream contains 3 elements, for instance, that would call Supplier.get()
3 times.
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.
Why are you flatmapping your "lazy" stream with Supplier get? If the this stream contains 3 elements, for instance, that would call Supplier.get() 3 times.
No, no - this Supplier::get
is called only on the lazy
variable, and not on the given supplier
. So it's always called only once because there is only one lazy
variable. But perhaphs I shouldn't have provided this implementation in such form, and I should have provided it using the proposed Seq.lazy
[#302]:
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 supplier
vs. Supplier
:)
As suggested by @johnmcclean-aol (https://github.com/jOOQ/jOOL/issues/177#issuecomment-170689447), 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
?