Created
March 27, 2017 14:32
-
-
Save thomasnield/342b2334e43a5c45ec487d5c5a9433bb to your computer and use it in GitHub Desktop.
RxJava Cartestian Product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io.reactivex.Observable | |
| fun main(args: Array<String>) { | |
| val sources = listOf( | |
| Observable.just(1, 2, 3, 4, 5), | |
| Observable.just(10, 20, 30, 40, 50), | |
| Observable.just(100, 200, 300, 400, 500) | |
| ) | |
| var cartesianProduct: Observable<List<Int>>? = null | |
| for (obs in sources) { | |
| if (cartesianProduct == null) { | |
| cartesianProduct = obs.map { listOf(it) } | |
| } else { | |
| cartesianProduct = cartesianProduct.flatMap { list -> obs.map { list.plus(it) }} | |
| } | |
| } | |
| cartesianProduct!!.subscribe(::println) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment