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

Add Seq<T> onEmptySwitch(Supplier<? extends Stream<? extends T>>) #179

Open
lukaseder opened this issue Jan 11, 2016 · 4 comments
Open

Add Seq<T> onEmptySwitch(Supplier<? extends Stream<? extends T>>) #179

lukaseder opened this issue Jan 11, 2016 · 4 comments

Comments

@lukaseder
Copy link
Member

As suggested by @johnmcclean-aol (#177 (comment)), an onEmptySwitch(Supplier<? extends Stream<? extends T>>) method might be useful in some situations.

Criticism:

  • The current onEmptyXXX() names were inspired by Optional.orElseXXX(). We should think about the term switch, or if there is any more suitable term for this action
  • Due to generic type erasure, we cannot overload a single onEmptySwitch(Supplier<E>) method with different bounds for <E>. What would be the best upper bound? Seq? Stream? Iterable?
@tlinkowski
Copy link

tlinkowski commented Apr 24, 2017

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?

@lukaseder
Copy link
Member Author

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.

@tlinkowski
Copy link

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();
    });
}

@lukaseder
Copy link
Member Author

Oh yeah, my bad. I get it. I was confused about the casing of supplier vs. Supplier :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants