1
1
package kotlinx.coroutines.flow.operators
2
2
3
+ import kotlinx.coroutines.FlowPreview
3
4
import kotlinx.coroutines.flow.Flow
4
5
import kotlinx.coroutines.flow.collect
5
6
import kotlinx.coroutines.flow.flow
@@ -9,11 +10,13 @@ import kotlin.math.min
9
10
10
11
/* *
11
12
* 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].
13
14
*
14
15
* @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.
15
16
*/
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() }
17
20
18
21
/* *
19
22
* 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()
23
26
* You should not store it or allow it to escape in some way, unless you made a snapshot of it.
24
27
* The last list may have less elements than the given [size].
25
28
*
26
- * This is slightly faster , than using flow.chunked(n).map { ... }
29
+ * This is more efficient , than using flow.chunked(n).map { ... }
27
30
*
28
31
* @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.
29
32
*/
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 > {
31
36
require(size > 0 ) { " Size should be greater than 0, but was $size " }
32
37
return windowed(size, size, true , transform)
33
38
}
@@ -44,7 +49,9 @@ fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>) -> R): Flow<R
44
49
* @param step the number of elements to move the window forward by on an each step
45
50
* @param partialWindows controls whether or not to keep partial windows in the end if any.
46
51
*/
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 >> =
48
55
windowed(size, step, partialWindows) { it.toList() }
49
56
50
57
/* *
@@ -56,14 +63,16 @@ fun <T> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean): Flow<Li
56
63
* You should not store it or allow it to escape in some way, unless you made a snapshot of it.
57
64
* Several last lists may have less elements than the given [size].
58
65
*
59
- * This is slightly faster , than using flow.windowed(...).map { ... }
66
+ * This is more efficient , than using flow.windowed(...).map { ... }
60
67
*
61
68
* Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
62
69
* @param size the number of elements to take in each window
63
70
* @param step the number of elements to move the window forward by on an each step.
64
71
* @param partialWindows controls whether or not to keep partial windows in the end if any.
65
72
*/
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 > {
67
76
require(size > 0 && step > 0 ) { " Size and step should be greater than 0, but was size: $size , step: $step " }
68
77
69
78
return flow {
0 commit comments