Skip to content

Passing composite data through flatMap and similar operators without creating container objects #2931

@jondwillis

Description

@jondwillis

Take the following contrived example:

        Observable
                .just("yo")
                .flatMap(s -> Observable.range(0, 100))
                .subscribe(integer -> Ln.d("Here's an Integer(%s), but how do I access that juicy String?", integer));

I often come up against this problem, where I'd like to act upon multiple chained streams. I can easily imagine a couple of ways to access both the String Observable and the Integer Observable within a downstream subscribe() or some other operator. One is pulling the String Observable into a variable and then accessing it within a lambda. Another is to do something like:

        Observable
                .just("yo")
                .flatMap(s -> 
                        Observable.combineLatest(
                                Observable.just(s),
                                Observable.range(0, 100),
                                Pair::new))
                .subscribe(pair -> Ln.d("Here's an Integer(%s), and here's a juicy String(%s), but isn't this a little hard to follow and annoying?", pair.second, pair.first));

But this necessitates repackaging the original Observable with the dependent Observable into some more complex data structure.

There's gotta be a better way. Help?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions