chaimt
chaimt

Reputation: 89

RxJava: chaining results from map methods

I am using RxJava for some time and have gotten stuck on the following pattern that repeats itself:

source
 .map(a -> b)
 .map(b -> c)
 .map(d -> func(a,b,c))

As you can see I need in a lower point of the chain some of results from the previous mapping methods. I am not sure if this is a java limitation since lambda's cannot reference external parameters that are not final, or this is a build in limitation to Rx-Extensions. My current solution is to use a wrapper that that holds a,b,c - and to add each one within the map, and then return the wrapper class. But this solution does not feel write.

Is there a better solution?

Upvotes: 1

Views: 69

Answers (1)

akarnokd
akarnokd

Reputation: 70017

If the maps follow each other directly, you can compact them into a single map and have all intermediate data available to the final function:

source.map(a -> {
    T b = fa(a);
    U c = fc(b);
    return func(a, b, c);
})

Where T and U are the types of the intermediate values of b and c respectively.

Upvotes: 3

Related Questions