Skip to content

Instantly share code, notes, and snippets.

@muthuraj57
Created September 19, 2017 07:09
Show Gist options
  • Save muthuraj57/a4d07f9922c5a5bc054c24feb3179fc1 to your computer and use it in GitHub Desktop.
Save muthuraj57/a4d07f9922c5a5bc054c24feb3179fc1 to your computer and use it in GitHub Desktop.
Variant of scan operator but supports different return type than source observable. Used mainly to calculate Diff for recyclerView.
inline fun <T, R> Observable<List<T>>.scanMap(crossinline func2: (List<T>, List<T>) -> R): Observable<R> {
return this.startWith(emptyList<T>()) //emit a empty list first, otherwise the .buffer() below won't emit at first (needs 2 emissions to emit)
.buffer(2, 1) //buffer the previous and current emission
.filter { it.size >= 2 }
.map { func2.invoke(it[0], it[1]) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment