Replacing Rx operators using Kotlin operators

Kiran Bablani
MindOrks
Published in
2 min readAug 25, 2019

Well in our Android Application we have n number of network calls and many developers used “Reactive Programming(RxJava/RxAndroid)” for Api calls. But thanks to Kotlin which has provided same set of operators . Why Rx now?? Let’s see how Kotlin replace the Rx operators.

Let’s suppose I have an Api call from which I need to return list of MyGist.

val observable =  RetrofitSingleton.getInstance()!!.provideClient()
.gistsObservable //returning Observable
fun provideClient(){
val retrofit = Retrofit.Builder().baseUrl("https://api.github.com")
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create()).build()
client = retrofit.create(GistApi::class.java)
}
val deferred = RetrofitSingleton.getInstance()!!.provideCoroutineClient()
.gistsDeferred //returning Deferred
val listOfItems = deferred.await() // returns List<Gist>
private fun provideCoroutineClient(){
val retrofit2 = Retrofit.Builder().baseUrl("https://api.github.com")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create()).build()
return retrofit2.create(GistApi::class.java)
}
  1. Flatmap

In Rx:

observable
.flatMap(Func1<ArrayList<Gist>, Observable<out Gist>> { Observable.from(it) })

In Kotlin:

listOfItems.flatMap { listOf(it)}  //simply converting it into list you can apply function on it

2. Filter

In Rx:

observable
.flatMap(Func1<ArrayList<Gist>, Observable<out Gist>> { Observable.from(it) })
.filter { it.owner != null }

In Kotlin:

listOfItems.filter {
it
.owner != null
}

filter internally is Extension Function of Iterable<T> which iterate over list filtering it and returns filtered list.

3. Zip

In Rx:

Observable.zip(
observable,
loadFeedviaRx2(),
Func2<ArrayList<Gist>, ArrayList<Gist>, ArrayList<Gist>> { list1, list2 ->
// here we get both the results at a time.
return@Func2 combinedList(list1,list2)
})
fun loadFeedviaRx2(): Observable<ArrayList<Gist>>? {

return RetrofitSingleton.getInstance()!!.provideClient()
.gistsObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

}

In Kotlin:

listOfItems.zip(loadFeedWithoutRx()) // returns  List<Pair<Gist, MyGist>>suspend fun loadFeedWithoutRx(): ArrayList<MyGist> {
return listOfItems

4. Map

I need to map Gist to MyGist

In Rx:

observable.flatMap(Func1<ArrayList<Gist>, Observable<out Gist>> { Observable.from(it) })
.map(Func1<Gist, MyGist> { MyGist(it) })

In Kotlin:

listOfItems
.map {
MyGist(it)
}

5. Distinct

In Rx:

observable.observeOn(AndroidSchedulers.mainThread())
.map(Func1<Gist, MyGist> { MyGist(it) })
.toList()
.distinct()

In Kotlin:

listOfItems.map {
MyGist(it)
}.distinct()

5. Buffer(Splitting into chunks)

In Rx:

 observable
.flatMap(Func1<ArrayList<Gist>, Observable<out Gist>> { Observable.from(it) })
.filter { it.owner != null }
.map(Func1<Gist, MyGist> { MyGist(it) })
.toList()
.buffer(3) //returns Observable<MutableList<MutableList<MyGist>>>?

In Kotlin:

listOfItems.filter {
it
.owner != null
}.map {
MyGist(it)
}.chunked(3) //returns ArrayList<List<MyGist>>

--

--