//Extensions for Jetpack Lifecycle/it.czerwinski.android.lifecycle.livedata/reduce

reduce

[androidJvm]
Content
fun <T> LiveData<T?>.reduce(operation: (T?, T?) -> T?): LiveData<T?>
More info

Returns a LiveData emitting accumulated value starting with the first value emitted by this LiveData and applying operation from left to right to current accumulator value and each value emitted by this.

operation will be executed on the main thread.

Example:

val newOperationsCountLiveData: LiveData<Int?> = …
val operationsCountLiveData: LiveData<Int?> =
newOperationsCountLiveData.reduce { acc, next -> if (next == null) null else acc + next }