Skip to content

Replace scanReduce with runningReduce to be consistent with Kotlin st… #2139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kotlinx-coroutines-core/api/kotlinx-coroutines-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ public final class kotlinx/coroutines/flow/FlowKt {
public static synthetic fun retry$default (Lkotlinx/coroutines/flow/Flow;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public static synthetic fun retry$default (Lkotlinx/coroutines/flow/Flow;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
public static final fun retryWhen (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function4;)Lkotlinx/coroutines/flow/Flow;
public static final fun runningReduce (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
public static final fun sample (Lkotlinx/coroutines/flow/Flow;J)Lkotlinx/coroutines/flow/Flow;
public static final fun sample-8GFy2Ro (Lkotlinx/coroutines/flow/Flow;D)Lkotlinx/coroutines/flow/Flow;
public static final fun scan (Lkotlinx/coroutines/flow/Flow;Ljava/lang/Object;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
Expand Down
8 changes: 8 additions & 0 deletions kotlinx-coroutines-core/common/src/flow/Migration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,11 @@ public fun <T> Flow<T>.delayEach(timeMillis: Long): Flow<T> = onEach { delay(tim
replaceWith = ReplaceWith("this.flatMapLatest(transform)")
)
public fun <T, R> Flow<T>.switchMap(transform: suspend (value: T) -> Flow<R>): Flow<R> = flatMapLatest(transform)

@Deprecated(
level = DeprecationLevel.WARNING, // Since 1.3.8, was experimental when deprecated
message = "'scanReduce' was renamed to 'runningReduce' to be consistent with Kotlin standard library",
replaceWith = ReplaceWith("runningReduce(operation)")
)
@ExperimentalCoroutinesApi
public fun <T> Flow<T>.scanReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = runningReduce(operation)
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public fun <T, R> Flow<T>.scan(initial: R, @BuilderInference operation: suspend
*
* For example:
* ```
* flowOf(1, 2, 3, 4).scanReduce { (v1, v2) -> v1 + v2 }.toList()
* flowOf(1, 2, 3, 4).runningReduce { (v1, v2) -> v1 + v2 }.toList()
* ```
* will produce `[1, 3, 6, 10]`
*/
@ExperimentalCoroutinesApi
public fun <T> Flow<T>.scanReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = flow {
public fun <T> Flow<T>.runningReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T> = flow {
var accumulator: Any? = NULL
collect { value ->
accumulator = if (accumulator === NULL) {
Expand Down
10 changes: 5 additions & 5 deletions kotlinx-coroutines-core/common/test/flow/operators/ScanTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ScanTest : TestBase() {
@Test
fun testScan() = runTest {
val flow = flowOf(1, 2, 3, 4, 5)
val result = flow.scanReduce { acc, v -> acc + v }.toList()
val result = flow.runningReduce { acc, v -> acc + v }.toList()
assertEquals(listOf(1, 3, 6, 10, 15), result)
}

Expand All @@ -26,13 +26,13 @@ class ScanTest : TestBase() {
@Test
fun testNulls() = runTest {
val flow = flowOf(null, 2, null, null, null, 5)
val result = flow.scanReduce { acc, v -> if (v == null) acc else (if (acc == null) v else acc + v) }.toList()
val result = flow.runningReduce { acc, v -> if (v == null) acc else (if (acc == null) v else acc + v) }.toList()
assertEquals(listOf(null, 2, 2, 2, 2, 7), result)
}

@Test
fun testEmptyFlow() = runTest {
val result = emptyFlow<Int>().scanReduce { _, _ -> 1 }.toList()
val result = emptyFlow<Int>().runningReduce { _, _ -> 1 }.toList()
assertTrue(result.isEmpty())
}

Expand All @@ -49,7 +49,7 @@ class ScanTest : TestBase() {
emit(1)
emit(2)
}
}.scanReduce { _, value ->
}.runningReduce { _, value ->
expect(value) // 2
latch.receive()
throw TestException()
Expand All @@ -59,7 +59,7 @@ class ScanTest : TestBase() {
finish(4)
}

public operator fun <T> Collection<T>.plus(element: T): List<T> {
private operator fun <T> Collection<T>.plus(element: T): List<T> {
val result = ArrayList<T>(size + 1)
result.addAll(this)
result.add(element)
Expand Down