Skip to content

Instantly share code, notes, and snippets.

@StuStirling
Created February 16, 2021 08:46
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 StuStirling/94225ba25d40199dfa30aade50df5e4f to your computer and use it in GitHub Desktop.
Save StuStirling/94225ba25d40199dfa30aade50df5e4f to your computer and use it in GitHub Desktop.
Flowable that emits previous item as well as latest.
data class PreviousAndLatest<T>(
var previous: T?,
var latest: T?
) {
fun addLatest(newLatest: T): PreviousAndLatest<T> {
if (latest != null) previous = latest
latest = newLatest
return this
}
}
flowable
.distinctUntilChanged()
.scanWith({ PreviousAndLatest<RandomClass>(null, null) }, { previous, latest ->
previous.addLatest(latest)
})
.filter { it.latest != null }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment