Skip to content

Commit 5e91dc4

Browse files
authored
Replace scanReduce with runningReduce to be consistent with Kotlin standard library. (#2139)
foldReduce is not introduced to see how it goes in the standard library first. For the full rationale of renaming, please refer to KT-38060
1 parent b42f986 commit 5e91dc4

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

kotlinx-coroutines-core/api/kotlinx-coroutines-core.api

+1
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ public final class kotlinx/coroutines/flow/FlowKt {
971971
public static synthetic fun retry$default (Lkotlinx/coroutines/flow/Flow;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
972972
public static synthetic fun retry$default (Lkotlinx/coroutines/flow/Flow;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
973973
public static final fun retryWhen (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function4;)Lkotlinx/coroutines/flow/Flow;
974+
public static final fun runningReduce (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
974975
public static final fun sample (Lkotlinx/coroutines/flow/Flow;J)Lkotlinx/coroutines/flow/Flow;
975976
public static final fun sample-8GFy2Ro (Lkotlinx/coroutines/flow/Flow;D)Lkotlinx/coroutines/flow/Flow;
976977
public static final fun scan (Lkotlinx/coroutines/flow/Flow;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;

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

+8
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,11 @@ public fun <T> Flow<T>.delayEach(timeMillis: Long): Flow<T> = onEach { delay(tim
430430
replaceWith = ReplaceWith("this.flatMapLatest(transform)")
431431
)
432432
public fun <T, R> Flow<T>.switchMap(transform: suspend (value: T) -> Flow<R>): Flow<R> = flatMapLatest(transform)
433+
434+
@Deprecated(
435+
level = DeprecationLevel.WARNING, // Since 1.3.8, was experimental when deprecated
436+
message = "'scanReduce' was renamed to 'runningReduce' to be consistent with Kotlin standard library",
437+
replaceWith = ReplaceWith("runningReduce(operation)")
438+
)
439+
@ExperimentalCoroutinesApi
440+
public fun <T> Flow<T>.scanReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = runningReduce(operation)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ public fun <T, R> Flow<T>.scan(initial: R, @BuilderInference operation: suspend
100100
*
101101
* For example:
102102
* ```
103-
* flowOf(1, 2, 3, 4).scanReduce { (v1, v2) -> v1 + v2 }.toList()
103+
* flowOf(1, 2, 3, 4).runningReduce { (v1, v2) -> v1 + v2 }.toList()
104104
* ```
105105
* will produce `[1, 3, 6, 10]`
106106
*/
107107
@ExperimentalCoroutinesApi
108-
public fun <T> Flow<T>.scanReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = flow {
108+
public fun <T> Flow<T>.runningReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = flow {
109109
var accumulator: Any? = NULL
110110
collect { value ->
111111
accumulator = if (accumulator === NULL) {

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

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

@@ -26,13 +26,13 @@ class ScanTest : TestBase() {
2626
@Test
2727
fun testNulls() = runTest {
2828
val flow = flowOf(null, 2, null, null, null, 5)
29-
val result = flow.scanReduce { acc, v -> if (v == null) acc else (if (acc == null) v else acc + v) }.toList()
29+
val result = flow.runningReduce { 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>().scanReduce { _, _ -> 1 }.toList()
35+
val result = emptyFlow<Int>().runningReduce { _, _ -> 1 }.toList()
3636
assertTrue(result.isEmpty())
3737
}
3838

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

62-
public operator fun <T> Collection<T>.plus(element: T): List<T> {
62+
private operator fun <T> Collection<T>.plus(element: T): List<T> {
6363
val result = ArrayList<T>(size + 1)
6464
result.addAll(this)
6565
result.add(element)

0 commit comments

Comments
 (0)