Skip to content

Instantly share code, notes, and snippets.

@vinaysshenoy
Created April 28, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinaysshenoy/f05a4d707529d936e1fa9cf2e940de63 to your computer and use it in GitHub Desktop.
Save vinaysshenoy/f05a4d707529d936e1fa9cf2e940de63 to your computer and use it in GitHub Desktop.
Using reduce to find a single element in an Observable chain
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