Skip to content

Commit dbe84ea

Browse files
committed
Add changes requested by R Elizarov
Add @FlowPreview Add public modifier Minor improvement in docs
1 parent 86a503d commit dbe84ea

File tree

1 file changed

+16
-7
lines changed
  • kotlinx-coroutines-core/common/src/flow/operators

1 file changed

+16
-7
lines changed

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package kotlinx.coroutines.flow.operators
22

3+
import kotlinx.coroutines.FlowPreview
34
import kotlinx.coroutines.flow.Flow
45
import kotlinx.coroutines.flow.collect
56
import kotlinx.coroutines.flow.flow
@@ -9,11 +10,13 @@ import kotlin.math.min
910

1011
/**
1112
* Returns a flow of lists each not exceeding the given [size].
12-
*The last list in the resulting flow may have less elements than the given [size].
13+
* The last list in the resulting flow may have less elements than the given [size].
1314
*
1415
* @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this flow.
1516
*/
16-
fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = chunked(size) { it.toList() }
17+
18+
@FlowPreview
19+
public fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = chunked(size) { it.toList() }
1720

1821
/**
1922
* Chunks a flow of elements into flow of lists, each not exceeding the given [size]
@@ -23,11 +26,13 @@ fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = chunked(size) { it.toList()
2326
* You should not store it or allow it to escape in some way, unless you made a snapshot of it.
2427
* The last list may have less elements than the given [size].
2528
*
26-
* This is slightly faster, than using flow.chunked(n).map { ... }
29+
* This is more efficient, than using flow.chunked(n).map { ... }
2730
*
2831
* @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this flow.
2932
*/
30-
fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>) -> R): Flow<R> {
33+
34+
@FlowPreview
35+
public fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>) -> R): Flow<R> {
3136
require(size > 0) { "Size should be greater than 0, but was $size" }
3237
return windowed(size, size, true, transform)
3338
}
@@ -44,7 +49,9 @@ fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>) -> R): Flow<R
4449
* @param step the number of elements to move the window forward by on an each step
4550
* @param partialWindows controls whether or not to keep partial windows in the end if any.
4651
*/
47-
fun <T> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean): Flow<List<T>> =
52+
53+
@FlowPreview
54+
public fun <T> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean): Flow<List<T>> =
4855
windowed(size, step, partialWindows) { it.toList() }
4956

5057
/**
@@ -56,14 +63,16 @@ fun <T> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean): Flow<Li
5663
* You should not store it or allow it to escape in some way, unless you made a snapshot of it.
5764
* Several last lists may have less elements than the given [size].
5865
*
59-
* This is slightly faster, than using flow.windowed(...).map { ... }
66+
* This is more efficient, than using flow.windowed(...).map { ... }
6067
*
6168
* Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
6269
* @param size the number of elements to take in each window
6370
* @param step the number of elements to move the window forward by on an each step.
6471
* @param partialWindows controls whether or not to keep partial windows in the end if any.
6572
*/
66-
fun <T, R> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean, transform: suspend (List<T>) -> R): Flow<R> {
73+
74+
@FlowPreview
75+
public fun <T, R> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean, transform: suspend (List<T>) -> R): Flow<R> {
6776
require(size > 0 && step > 0) { "Size and step should be greater than 0, but was size: $size, step: $step" }
6877

6978
return flow {

0 commit comments

Comments
 (0)