Skip to content
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

Add plus operator idea from concat #192

Merged
merged 12 commits into from
Nov 11, 2023
5 changes: 1 addition & 4 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

- Add `Flow.zipWithNext()` operator, it is an alias to `Flow.pairwise()` operator.
- Add `Flow.zipWithNext(transform)` operator, it is an alias to `Flow.pairwise(transform)` operator.
- Add `Flow.plus` operator.
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved

## [0.7.2] - Oct 7, 2023

Expand Down
23 changes: 23 additions & 0 deletions README.md
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ dependencies {
- [`amb`](#race--amb)
- [`range`](#range)
- [`timer`](#timer)
- [`plus`](#plus)
hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved

- Intermediate operators
- [`bufferCount`](#buffercount--chunked)
Expand Down Expand Up @@ -1303,6 +1304,28 @@ withLatestFrom: (2, 3)
withLatestFrom: (3, 4)
withLatestFrom: (4, 6)
```
#### plus

Concat two [Flow]s of the same base type [T] into a single [Flow] by concatenating their elements.

```kotlin
val flow1 = flowOf(1, 2, 3)
val flow2 = flowOf(4, 5, 6)

(flow1 + flow2).collect { println("plus: $it") }
```

Output:

```none
plus: 1
plus: 2
plus: 3
plus: 4
plus: 5
plus: 6
```

hoangchungk53qx1 marked this conversation as resolved.
Show resolved Hide resolved

... and more, please check out [Docs 0.x](https://hoc081098.github.io/FlowExt/docs/0.x)/[Docs
snapshot](https://hoc081098.github.io/FlowExt/docs/latest).
Expand Down
4 changes: 4 additions & 0 deletions api/FlowExt.api
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ public final class com/hoc081098/flowext/PairwiseKt {
public static final fun zipWithNext (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
}

public final class com/hoc081098/flowext/PlusKt {
public static final fun plus (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;)Lkotlinx/coroutines/flow/Flow;
}

public final class com/hoc081098/flowext/RaceKt {
public static final fun amb (Ljava/lang/Iterable;)Lkotlinx/coroutines/flow/Flow;
public static final fun amb (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;[Lkotlinx/coroutines/flow/Flow;)Lkotlinx/coroutines/flow/Flow;
Expand Down
43 changes: 43 additions & 0 deletions src/commonMain/kotlin/com/hoc081098/flowext/plus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
* MIT License
*
* Copyright (c) 2021-2023 Petrus Nguyễn Thái Học
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.hoc081098.flowext

import kotlinx.coroutines.flow.Flow

/**
* Combines two [Flow]s of the same base type [T] into a single [Flow] by concatenating their elements.
* @param other The [Flow] to concatenate with the current [Flow].
* @return A new [Flow] containing the concatenated elements of both the current and the [other] [Flow]s.
*
* Example:
*
* ``` kotlin
* val flow1 = flowOf(1, 2, 3)
* val flow2 = flowOf(4, 5, 6)
* val plusFlow = flow1 + flow2 //1, 2, 3, 4, 5, 6
* ```
*/

public operator fun <T, R : T> Flow<T>.plus(other: Flow<R>): Flow<T> = concat(this, other)
94 changes: 94 additions & 0 deletions src/commonTest/kotlin/com/hoc081098/flowext/PlusTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* MIT License
*
* Copyright (c) 2021-2023 Petrus Nguyễn Thái Học
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.hoc081098.flowext

import com.hoc081098.flowext.utils.BaseTest
import com.hoc081098.flowext.utils.TestException
import com.hoc081098.flowext.utils.test
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf

@OptIn(ExperimentalCoroutinesApi::class)
class PlusTest : BaseTest() {
@Test
fun plusTwoFlow() = runTest {
val flow1 = flowOf(1, 2, 3)
val flow2 = flowOf(4, 5, 6)

(flow1 + flow2)
.test(
listOf(
Event.Value(1),
Event.Value(2),
Event.Value(3),
Event.Value(4),
Event.Value(5),
Event.Value(6),
Event.Complete,
),
)
}

@Test
fun plusTwoFlow2() = runTest {
(flowOf("a", 2, 3) + flowOf(4, 5, 6))
.test(
listOf(
Event.Value("a"),
Event.Value(2),
Event.Value(3),
Event.Value(4),
Event.Value(5),
Event.Value(6),
Event.Complete,
),
)
}

@Test
fun testPlus_firstFailureUpstream() = runTest {
val flow = flowOf(1, 2, 3)
val failureFlow = kotlinx.coroutines.flow.flow<Nothing> { throw TestException("Crash!") }
val expectation: suspend (List<Event<Int>>) -> Unit = { events ->
val message = assertIs<TestException>(events.single().errorOrThrow()).message
assertEquals("Crash!", message)
}

(failureFlow + flow)
.test(null, expectation)

(failureFlow + flow + flow)
.test(null, expectation)

(failureFlow + flow + flow + flow)
.test(null, expectation)

(failureFlow + flow + flow + flow + flow)
.test(null, expectation)
}
}