Maintain previous value in flatmap

With RxJava and chaining operators it is not straightforward how to reuse the previous value of a flatMap operation. The usual way to go about this is to use a member variable or instance variable which can temporary store the value we need to keep which is not the best way to solve since we don’t want to introduce state inside the functional/reactive approach of RxJava. The developers of RxJava have thought about this and there is an overloaded method of flatMap which takes a Func2 as its 2nd parameter and returns an Observable which emits the results of a specified function to the pair of values emitted by the source.

For instance you can use it like this:

                .flatMap({ Observable.just(1)}) { string, number -> Pair(string, number) }
                .subscribe { (first, second) ->
                    println("Original Value: $first Number: $second") }