Created
September 19, 2017 07:09
-
-
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.
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
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