Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Created March 27, 2017 14:32
Show Gist options
  • Select an option

  • Save thomasnield/342b2334e43a5c45ec487d5c5a9433bb to your computer and use it in GitHub Desktop.

Select an option

Save thomasnield/342b2334e43a5c45ec487d5c5a9433bb to your computer and use it in GitHub Desktop.
RxJava Cartestian Product
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