Take operators (take, takeLast, takeWhile, and takeUntil)

The take operators work in exactly the opposite way than the skip operators. Let's take an example of them one by one and understand how they work:

 fun main(args: Array<String>) { val observable1 = Observable.range(1,20) observable1 .take(5)//(1) .subscribe(object:Observer<Int> { override fun onError(e: Throwable) { println("Error $e") } override fun onComplete() { println("Complete") } override fun onNext(t: Int) { println("Received $t") } override fun onSubscribe(d: Disposable) { println("starting skip(count)") } }) val observable2 = Observable.interval(100,TimeUnit.MILLISECONDS) observable2 .take(400,TimeUnit.MILLISECONDS)//(2) .subscribe( object:Observer<Long> { override fun onError(e: ...

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.