Filtering emissions - filter operator

The filter operator is arguably the most used filtering/suppressing operator. It lets you implement custom logic to filter emissions.

The following code snippet is the simplest implementation of the filter operator:

    fun main(args: Array<String>) { 
      Observable.range(1,20)//(1) 
        .filter{//(2) 
          it%2==0 
     }      .subscribe { 
         println("Received $it")  
      } 
   }

On comment (1), we created an Observable instance with the help of the Observable.range() operator. We filtered out odd numbers from the emissions with the help of the filter operator on comment (2).

The following is the output:

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.