Skip to content

Commit 6b3b10e

Browse files
committed
Renaming
1 parent 2d2a1b7 commit 6b3b10e

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,8 @@ 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/Function3;)Lkotlinx/coroutines/flow/Flow;
859+
public static final fun scan (Lkotlinx/coroutines/flow/Flow;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
860+
public static final fun scanReduce (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
860861
public static final fun single (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
861862
public static final fun singleOrNull (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
862863
public static final fun switchMap (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/flow/Flow;
@@ -885,6 +886,7 @@ public final class kotlinx/coroutines/flow/MigrationKt {
885886
public static final fun observeOn (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow;
886887
public static final fun onErrorResume (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;)Lkotlinx/coroutines/flow/Flow;
887888
public static final fun publishOn (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow;
889+
public static final fun scanFold (Lkotlinx/coroutines/flow/Flow;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
888890
public static final fun skip (Lkotlinx/coroutines/flow/Flow;I)Lkotlinx/coroutines/flow/Flow;
889891
public static final fun subscribe (Lkotlinx/coroutines/flow/Flow;)V
890892
public static final fun subscribe (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function1;)V

kotlinx-coroutines-core/common/src/flow/Migration.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,11 @@ public fun <T> Flow<T>.skip(count: Int): Flow<T> = error("Should not be called")
239239
message = "Flow analogue of 'forEach' is 'collect'",
240240
replaceWith = ReplaceWith("collect(block)")
241241
)
242-
public fun <T> Flow<T>.forEach(action: suspend (value: T) -> Unit): Unit = error("Should not be called")
242+
public fun <T> Flow<T>.forEach(action: suspend (value: T) -> Unit): Unit = error("Should not be called")
243+
244+
@Deprecated(
245+
level = DeprecationLevel.ERROR,
246+
message = "Flow has less verbose 'scan' shortcut",
247+
replaceWith = ReplaceWith("scan(initial, operation)")
248+
)
249+
public fun <T, R> Flow<T>.scanFold(initial: R, @BuilderInference operation: suspend (accumulator: R, value: T) -> R): Flow<R> = error("Should not be called")

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

+21-21
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,29 @@ public fun <T> Flow<T>.onEach(action: suspend (T) -> Unit): Flow<T> = flow {
100100
}
101101
}
102102

103+
/**
104+
* Folds the given flow with [operation], emitting every intermediate result, including [initial] value.
105+
* Note that initial value should be immutable (or should not be mutated) as it is shared between different collectors.
106+
* For example:
107+
* ```
108+
* flowOf(1, 2, 3).accumulate(emptyList<Int>()) { acc, value -> acc + value }.toList()
109+
* ```
110+
* will produce `[], [1], [1, 2], [1, 2, 3]]`.
111+
*/
112+
@FlowPreview
113+
public fun <T, R> Flow<T>.scan(initial: R, @BuilderInference operation: suspend (accumulator: R, value: T) -> R): Flow<R> = flow {
114+
var accumulator: R = initial
115+
emit(accumulator)
116+
collect { value ->
117+
accumulator = operation(accumulator, value)
118+
emit(accumulator)
119+
}
120+
}
121+
103122
/**
104123
* Reduces the given flow with [operation], emitting every intermediate result, including initial value.
105124
* The first element is taken as initial value for operation accumulator.
106-
* This operator has a sibling with initial value -- [accumulate].
125+
* This operator has a sibling with initial value -- [scan].
107126
*
108127
* For example:
109128
* ```
@@ -112,7 +131,7 @@ public fun <T> Flow<T>.onEach(action: suspend (T) -> Unit): Flow<T> = flow {
112131
* will produce `[1, 3, 6, 10]`
113132
*/
114133
@FlowPreview
115-
public fun <T> Flow<T>.scan(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = flow {
134+
public fun <T> Flow<T>.scanReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = flow {
116135
var accumulator: Any? = NullSurrogate
117136
collect { value ->
118137
accumulator = if (accumulator === NullSurrogate) {
@@ -123,22 +142,3 @@ public fun <T> Flow<T>.scan(operation: suspend (accumulator: T, value: T) -> T):
123142
emit(accumulator as T)
124143
}
125144
}
126-
127-
/**
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.
130-
* For example:
131-
* ```
132-
* flowOf(1, 2, 3).accumulate(emptyList<Int>()) { acc, value -> acc + value }.toList()
133-
* ```
134-
* will produce `[], [1], [1, 2], [1, 2, 3]]`.
135-
*/
136-
@FlowPreview
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
139-
emit(accumulator)
140-
collect { value ->
141-
accumulator = operation(accumulator, value)
142-
emit(accumulator)
143-
}
144-
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ class ScanTest : TestBase() {
1212
@Test
1313
fun testScan() = runTest {
1414
val flow = flowOf(1, 2, 3, 4, 5)
15-
val result = flow.scan { acc, v -> acc + v }.toList()
15+
val result = flow.scanReduce { acc, v -> acc + v }.toList()
1616
assertEquals(listOf(1, 3, 6, 10, 15), result)
1717
}
1818

1919
@Test
2020
fun testScanWithInitial() = runTest {
2121
val flow = flowOf(1, 2, 3)
22-
val result = flow.accumulate(emptyList<Int>()) { acc, value -> acc + value }.toList()
22+
val result = flow.scan(emptyList<Int>()) { acc, value -> acc + value }.toList()
2323
assertEquals(listOf(emptyList(), listOf(1), listOf(1, 2), listOf(1, 2, 3)), result)
2424
}
2525

2626
@Test
2727
fun testNulls() = runTest {
2828
val flow = flowOf(null, 2, null, null, null, 5)
29-
val result = flow.scan { acc, v -> if (v == null) acc else (if (acc == null) v else acc + v) }.toList()
29+
val result = flow.scanReduce { acc, v -> if (v == null) acc else (if (acc == null) v else acc + v) }.toList()
3030
assertEquals(listOf(null, 2, 2, 2, 2, 7), result)
3131
}
3232

3333
@Test
3434
fun testEmptyFlow() = runTest {
35-
val result = emptyFlow<Int>().scan { _, _ -> 1 }.toList()
35+
val result = emptyFlow<Int>().scanReduce { _, _ -> 1 }.toList()
3636
assertTrue(result.isEmpty())
3737
}
3838

@@ -49,7 +49,7 @@ class ScanTest : TestBase() {
4949
emit(1)
5050
emit(2)
5151
}
52-
}.scan { _, value ->
52+
}.scanReduce { _, value ->
5353
expect(value) // 2
5454
latch.receive()
5555
throw TestException()

0 commit comments

Comments
 (0)