question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:26
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

10reactions
benjchristensencommented, May 7, 2015

Try this:

import rx.Observable;

public class ComposeViaMap {
    public static void main(String... args) {
        Observable
                .just("a", "b")
                .flatMap(s ->
                        Observable.range(0, 100)
                                .map(integer -> String.format("Here's an Integer(%s), with String(%s)", integer, s)))
                .subscribe(System.out::println);
    }
}
2reactions
akarnokdcommented, Feb 8, 2018

@amanshuraikwar There is a library that may be what you are looking for: https://github.com/pakoito/Komprehensions

Read more comments on GitHub >

github_iconTop Results From Across the Web

4. Applying Reactive Programming to Existing Applications
A similar operator, first() , will return a value of T and discard whatever it has left. single() , on the other hand,...
Read more >
Passing composite data in rxjs observable chains
You can use a map operator to combine the argument received by the flatMap projection function with the observable's result: getData() .
Read more >
Flux (reactor-core 3.5.0)
Build a Flux whose data are generated by the combination of the most recently published value from each of the Publisher sources provided...
Read more >
Flowable (RxJava Javadoc 3.1.5) - ReactiveX
The Flowable class that implements the Reactive Streams Publisher Pattern and offers factory methods, intermediate operators and the ability to consume ...
Read more >
X++ composite data types - Dynamics 365 - Microsoft Learn
A container is useful when you must pass various types of values between the client and server tiers. However, if you plan to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found