Skip to content

Instantly share code, notes, and snippets.

@joeyciechanowicz
Last active June 23, 2017 10:47
Show Gist options
  • Save joeyciechanowicz/246edcb97a9b58e549c61708f2cacbe3 to your computer and use it in GitHub Desktop.
Save joeyciechanowicz/246edcb97a9b58e549c61708f2cacbe3 to your computer and use it in GitHub Desktop.
/**
* 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