Skip to content

Commit

Permalink
Add changes requested by R Elizarov
Browse files Browse the repository at this point in the history
Add @FlowPreview
Add public modifier
Minor improvement in docs
  • Loading branch information
circusmagnus committed Mar 18, 2020
1 parent 86a503d commit dbe84ea
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions kotlinx-coroutines-core/common/src/flow/operators/Chunk.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kotlinx.coroutines.flow.operators

import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
Expand All @@ -9,11 +10,13 @@ import kotlin.math.min

/**
* Returns a flow of lists each not exceeding the given [size].
*The last list in the resulting flow may have less elements than the given [size].
* The last list in the resulting flow may have less elements than the given [size].
*
* @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.
*/
fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = chunked(size) { it.toList() }

@FlowPreview
public fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = chunked(size) { it.toList() }

/**
* Chunks a flow of elements into flow of lists, each not exceeding the given [size]
Expand All @@ -23,11 +26,13 @@ fun <T> Flow<T>.chunked(size: Int): Flow<List<T>> = chunked(size) { it.toList()
* You should not store it or allow it to escape in some way, unless you made a snapshot of it.
* The last list may have less elements than the given [size].
*
* This is slightly faster, than using flow.chunked(n).map { ... }
* This is more efficient, than using flow.chunked(n).map { ... }
*
* @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.
*/
fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>) -> R): Flow<R> {

@FlowPreview
public fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>) -> R): Flow<R> {
require(size > 0) { "Size should be greater than 0, but was $size" }
return windowed(size, size, true, transform)
}
Expand All @@ -44,7 +49,9 @@ fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>) -> R): Flow<R
* @param step the number of elements to move the window forward by on an each step
* @param partialWindows controls whether or not to keep partial windows in the end if any.
*/
fun <T> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean): Flow<List<T>> =

@FlowPreview
public fun <T> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean): Flow<List<T>> =
windowed(size, step, partialWindows) { it.toList() }

/**
Expand All @@ -56,14 +63,16 @@ fun <T> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean): Flow<Li
* You should not store it or allow it to escape in some way, unless you made a snapshot of it.
* Several last lists may have less elements than the given [size].
*
* This is slightly faster, than using flow.windowed(...).map { ... }
* This is more efficient, than using flow.windowed(...).map { ... }
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this collection.
* @param size the number of elements to take in each window
* @param step the number of elements to move the window forward by on an each step.
* @param partialWindows controls whether or not to keep partial windows in the end if any.
*/
fun <T, R> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean, transform: suspend (List<T>) -> R): Flow<R> {

@FlowPreview
public fun <T, R> Flow<T>.windowed(size: Int, step: Int, partialWindows: Boolean, transform: suspend (List<T>) -> R): Flow<R> {
require(size > 0 && step > 0) { "Size and step should be greater than 0, but was size: $size, step: $step" }

return flow {
Expand Down

0 comments on commit dbe84ea

Please sign in to comment.