Last active
June 23, 2017 10:47
-
-
Save joeyciechanowicz/246edcb97a9b58e549c61708f2cacbe3 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Emits repeated values only. | |
*/ | |
fun <T> repeats(): Observable.Transformer<T, T> = | |
Observable.Transformer<T, T> { observable -> | |
observable.scan(Pair(null, null), {previous: Pair<T?, T?>, current: T -> Pair(previous!!.second, current)}) | |
.filter { x -> x.first == x.second } // only let repeated values go through | |
.map { x -> x.first } // take the repeated value from the Pair and drop the Boolean | |
} | |
/** | |
* Example usage for tab bar selections. | |
*/ | |
Observable.just("Home", "Browse", "Home", "Home", "Profile", "Profile", "Home", "Browse", "Browse", "Profile") | |
.compose(repeats()) | |
.subscribe { selection -> toast("Clicked on $selection twice!") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment