Skip to content

Commit e118a9a

Browse files
committed
Rename scan to accumulate, get rid of lambda with initial value
1 parent 1f2c7f9 commit e118a9a

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,6 @@ public final class kotlinx/coroutines/flow/FlowKt {
856856
public static final fun retry (Lkotlinx/coroutines/flow/Flow;ILkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
857857
public static synthetic fun retry$default (Lkotlinx/coroutines/flow/Flow;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
858858
public static final fun sample (Lkotlinx/coroutines/flow/Flow;J)Lkotlinx/coroutines/flow/Flow;
859-
public static final fun scan (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
860859
public static final fun scan (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
861860
public static final fun single (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
862861
public static final fun singleOrNull (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;

kotlinx-coroutines-core/common/src/flow/operators/Transform.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public fun <T> Flow<T>.onEach(action: suspend (T) -> Unit): Flow<T> = flow {
102102

103103
/**
104104
* Reduces the given flow with [operation], emitting every intermediate result, including initial value.
105-
* The first element is takes as initial value for operation accumulator.
105+
* The first element is taken as initial value for operation accumulator.
106+
* This operator has a sibling with initial value -- [accumulate].
107+
*
106108
* For example:
107109
* ```
108110
* flowOf(1, 2, 3, 4).scan { (v1, v2) -> v1 + v2 }.toList()
@@ -123,17 +125,17 @@ public fun <T> Flow<T>.scan(operation: suspend (accumulator: T, value: T) -> T):
123125
}
124126

125127
/**
126-
* Reduces the given flow with [operation], emitting every intermediate result, including initial value.
127-
* An initial value is provided lazily by [initialSupplier] and is always immediately emitted.
128+
* Reduces the given flow with [operation], emitting every intermediate result, including [initial] value.
129+
* Note that initial value should be immutable (or should not be mutated) as it is shared between different collectors.
128130
* For example:
129131
* ```
130-
* flowOf(1, 2, 3).scan(::emptyList) { acc: List<Int>, value -> acc + value }.toList()
132+
* flowOf(1, 2, 3).accumulate(emptyList<Int>()) { acc, value -> acc + value }.toList()
131133
* ```
132134
* will produce `[], [1], [1, 2], [1, 2, 3]]`.
133135
*/
134136
@FlowPreview
135-
public fun <T, R> Flow<T>.scan(initialSupplier: () -> R, @BuilderInference operation: suspend (accumulator: R, value: T) -> R): Flow<R> = flow {
136-
var accumulator: R = initialSupplier()
137+
public fun <T, R> Flow<T>.accumulate(initial: R, @BuilderInference operation: suspend (accumulator: R, value: T) -> R): Flow<R> = flow {
138+
var accumulator: R = initial
137139
emit(accumulator)
138140
collect { value ->
139141
accumulator = operation(accumulator, value)

kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ public suspend inline fun <T> Flow<T>.collect(crossinline action: suspend (value
3737
* Collects all the values from the given [flow] and emits them to the collector.
3838
* Shortcut for `flow.collect { value -> emit(value) }`.
3939
*/
40-
public suspend inline fun <T> FlowCollector<T>.emitAll(flow: Flow<T>) = flow.collect { value -> emit(value) }
40+
public suspend inline fun <T> FlowCollector<T>.emitAll(flow: Flow<T>) = flow.collect(this)

kotlinx-coroutines-core/common/test/flow/operators/ScanTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ScanTest : TestBase() {
1919
@Test
2020
fun testScanWithInitial() = runTest {
2121
val flow = flowOf(1, 2, 3)
22-
val result = flow.scan(::emptyList) { acc: List<Int>, value -> acc + value }.toList()
22+
val result = flow.accumulate(emptyList<Int>()) { acc, value -> acc + value }.toList()
2323
assertEquals(listOf(emptyList(), listOf(1), listOf(1, 2), listOf(1, 2, 3)), result)
2424
}
2525

0 commit comments

Comments
 (0)