Skip to content

Instantly share code, notes, and snippets.

@gbajaj
Created July 2, 2025 05:01
Show Gist options
  • Save gbajaj/5e08c932294c2011558a575d2603d28b to your computer and use it in GitHub Desktop.
Save gbajaj/5e08c932294c2011558a575d2603d28b to your computer and use it in GitHub Desktop.
DistinctUntilChanged
fun <T> Flow<T>.distinctUntilChanged(comparator: (T, T) -> Boolean): Flow<T> = flow {
var previous: T? = null
var isFirst = true
collect { value ->
if (isFirst || !comparator(previous!!, value)) {
emit(value)
previous = value
isFirst = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment