Skip to content

Instantly share code, notes, and snippets.

@kittinunf
Created November 6, 2017 10:00
Show Gist options
  • Save kittinunf/4a202ef306ec3300692f2ac2c9b684f6 to your computer and use it in GitHub Desktop.
Save kittinunf/4a202ef306ec3300692f2ac2c9b684f6 to your computer and use it in GitHub Desktop.
class RxJavaActivity : AppCompatActivity() {
val cities = Observable.just("Warsaw", "Paris", "London", "Madrid", "Tokyo")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
concatMap()
// flatMap()
// concatMapEagerly()
}
fun concatMap() {
cities.concatMap(::populationOf)
.subscribe(this::print)
}
fun flatMap() {
cities.flatMap(::populationOf)
.subscribe(this::print)
}
fun concatMapEagerly() {
cities.concatMapEager(::populationOf)
.subscribe(this::print)
}
fun print(p: Int) {
println("Population: $p")
}
}
fun populationOf(city: String): Observable<Int> {
println("Request --> ../population?q=$city")
val map = mapOf("Warsaw" to 1702139, "Paris" to 2138551, "London" to 7556900, "Madrid" to 3255944, "Tokyo" to 27394596)
return Observable.just(map[city]!!)
.delay((500..1000).rand().toLong(), TimeUnit.MILLISECONDS)
.doOnNext {
println("Response <-- { $city : $it }")
}
}
fun IntRange.rand(): Int {
val r = Random()
return start + r.nextInt(last - start)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment