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

2.x: FlatMap Observable with a Single #4635

Closed
vanniktech opened this issue Sep 29, 2016 · 13 comments
Closed

2.x: FlatMap Observable with a Single #4635

vanniktech opened this issue Sep 29, 2016 · 13 comments

Comments

@vanniktech
Copy link
Collaborator

Often times I have an Observable and want to flatMap all values with a Single.

Imagine having Single<Photo> getPhoto(int user)

if I wanted to flatMap it with an Observable it would look like this:

Observable.range(1, 100).flatMap(user -> getPhoto(user).toObservable())

It does the job however the Single always needs to be converted to an Observable. Are you guys open for a flatMap version which takes a Single or is there any way around this already without the need to convert the Single to an Observable?

@akarnokd
Copy link
Member

Possibly; but the overall problem is the combinatorial explosion as you'd need mapping with Maybe, Completable herea nd extend other operators, as @JakeWharton warned about.

@vanniktech
Copy link
Collaborator Author

Yeah I already had this regard in my mind, basically 6 new API calls would need to be created:

Flowable.flatMapSingle()
Flowable.flatMapCompletable()
Flowable.flatMapMaybe()
Observable.flatMapSingle()
Observable.flatMapCompletable()
Observable.flatMapMaybe()

I understand the concern about all of the varieties however personally I feel like this would be a good addition to the public API. Especially since Completable, Maybe and Single already have those flatMap variants. Also with having those special flatMaps one would not need to convert the original Type to an Observable / Flowable anymore.

I'd be happy to contribute to those.

@akarnokd
Copy link
Member

Sure. Also could you please add comparative benchmarks?

@vanniktech
Copy link
Collaborator Author

You mean comparing the new methods against the old flatMap(single.toObservable()) plus the other types?

@akarnokd
Copy link
Member

Yes.

@akarnokd akarnokd added this to the 2.0 RC4 milestone Sep 29, 2016
@vanniktech
Copy link
Collaborator Author

Alright I'll start with Observable.flatMapSingle() though it might take me a while to grasp all of the internals of flatMap

@LalitMaganti
Copy link
Contributor

I'm also working on the idea described in #4584 (at https://github.com/tilal6991/RxJava/commits/2.x) to try and get rid of this combinatorial explosion - this is especially valuable if you find that hand optimizing for every variant turns out not to be worth the effort.

@akarnokd
Copy link
Member

The patterns will be very similar but due to the upstream and receiver types, you can't have one implementation.

@LalitMaganti
Copy link
Contributor

I understand - this is more to reduce the fact that all the classes are spread across the codebase without any sharing of implementation. I'm pretty sure we can consolidate code somewhat.

@vanniktech
Copy link
Collaborator Author

Schedule has gotten tighter I don't think I can come up with anything until RC4 (this Thursday)

@akarnokd
Copy link
Member

akarnokd commented Oct 4, 2016

Okay, I'll do them.

@akarnokd
Copy link
Member

akarnokd commented Oct 4, 2016

Done in #4667; It's easy to write them because knowing that there's going to be at most 1 element opens up nice simplifications.

@vanniktech
Copy link
Collaborator Author

Closing via #4667. Thanks once again

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

3 participants