The map operator

The map operator performs a given task (lambda) on each of the emitted items and emits them to the downstream. We have already seen a little use of the map operator. For a given Observable<T> or Flowable<T>, the map operator will transform an emitted item of type T into an emission of type R by applying the provided lambda of Function<T,R> to it.

So, now, let's take a look at another example with the map operator:

    fun main(args: Array<String>) { 
      val observable = listOf(10,9,8,7,6,5,4,3,2,1).toObservable() 
      observable.map {//(1) 
        number-> "Transforming Int to String $number" 
      }.subscribe { 
        item-> println("Received $item") 
      } 
    }

On comment (1), we used the map operator, which will transform the emitted item of type Int to an emission ...

Get Reactive Programming in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.