Created
April 28, 2018 06:49
-
-
Save vinaysshenoy/f05a4d707529d936e1fa9cf2e940de63 to your computer and use it in GitHub Desktop.
Using reduce to find a single element in an Observable chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.reactivex.Observable | |
data class Item(val priority: Int) | |
Observable.fromArray(Item(10), Item(5), Item(25)) | |
.reduce { first, next -> if(first.priority < next.priority) first else next } | |
.toSingle() //This converts a Maybe to a Single that will emit an error if there are no elements in the stream | |
.subscribe({ println("Least priority: $it") }, {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment