Docs and samples for 'chunked' function

This commit is contained in:
Ilya Gorbunov
2017-05-16 23:53:46 +03:00
parent e418f61f0c
commit b2f2e3537b
12 changed files with 391 additions and 2 deletions
@@ -1798,11 +1798,34 @@ public fun <T : Any> List<T?>.requireNoNulls(): List<T> {
return this as List<T>
}
/**
* Splits this collection into a list of lists each not exceeding the given [size].
*
* The last list in the resulting list 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 collection.
*
* @sample samples.collections.Collections.Transformations.chunked
*/
@SinceKotlin("1.2")
public fun <T> Iterable<T>.chunked(size: Int): List<List<T>> {
return windowed(size, size)
}
/**
* Splits this collection into several lists each not exceeding the given [size]
* and applies the given [transform] function to an each.
*
* @return list of results of the [transform] applied to an each list.
*
* Note that the list passed to the [transform] function is ephemeral and is valid only inside that function.
* You should not store it or allow it escape someway, unless you made a snapshot of it.
* The last list 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 collection.
*
* @sample samples.text.Strings.chunkedTransform
*/
@SinceKotlin("1.2")
public fun <T, R> Iterable<T>.chunked(size: Int, transform: (List<T>) -> R): List<R> {
return windowed(size, size, transform)
@@ -1331,11 +1331,38 @@ public fun <T : Any> Sequence<T?>.requireNoNulls(): Sequence<T> {
return map { it ?: throw IllegalArgumentException("null element found in $this.") }
}
/**
* Splits this sequence into a sequence of lists each not exceeding the given [size].
*
* The last list in the resulting sequence 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 sequence.
*
* @sample samples.collections.Collections.Transformations.chunked
*
* The operation is _intermediate_ and _stateful_.
*/
@SinceKotlin("1.2")
public fun <T> Sequence<T>.chunked(size: Int): Sequence<List<T>> {
return windowed(size, size)
}
/**
* Splits this sequence into several lists each not exceeding the given [size]
* and applies the given [transform] function to an each.
*
* @return sequence of results of the [transform] applied to an each list.
*
* Note that the list passed to the [transform] function is ephemeral and is valid only inside that function.
* You should not store it or allow it escape someway, unless you made a snapshot of it.
* The last list 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 sequence.
*
* @sample samples.text.Strings.chunkedTransform
*
* The operation is _intermediate_ and _stateful_.
*/
@SinceKotlin("1.2")
public fun <T, R> Sequence<T>.chunked(size: Int, transform: (List<T>) -> R): Sequence<R> {
return windowed(size, size, transform)
@@ -1110,21 +1110,67 @@ public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double {
return sum
}
/**
* Splits this char sequence into a list of strings each not exceeding the given [size].
*
* The last string in the resulting list may have less characters than the given [size].
*
* @param size the number of elements to take in each string, must be positive and can be greater than the number of elements in this char sequence.
*
* @sample samples.collections.Collections.Transformations.chunked
*/
@SinceKotlin("1.2")
public fun CharSequence.chunked(size: Int): List<String> {
return windowed(size, size)
}
/**
* Splits this char sequence into several char sequences each not exceeding the given [size]
* and applies the given [transform] function to an each.
*
* @return list of results of the [transform] applied to an each char sequence.
*
* Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function.
* You should not store it or allow it escape someway, unless you made a snapshot of it.
* The last char sequence may have less characters than the given [size].
*
* @param size the number of elements to take in each char sequence, must be positive and can be greater than the number of elements in this char sequence.
*
* @sample samples.text.Strings.chunkedTransform
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.chunked(size: Int, transform: (CharSequence) -> R): List<R> {
return windowed(size, size, transform)
}
/**
* Splits this char sequence into a sequence of strings each not exceeding the given [size].
*
* The last string in the resulting sequence may have less characters than the given [size].
*
* @param size the number of elements to take in each string, must be positive and can be greater than the number of elements in this char sequence.
*
* @sample samples.collections.Collections.Transformations.chunked
*/
@SinceKotlin("1.2")
public fun CharSequence.chunkedSequence(size: Int): Sequence<String> {
return chunkedSequence(size) { it.toString() }
}
/**
* Splits this char sequence into several char sequences each not exceeding the given [size]
* and applies the given [transform] function to an each.
*
* @return sequence of results of the [transform] applied to an each char sequence.
*
* Note that the char sequence passed to the [transform] function is ephemeral and is valid only inside that function.
* You should not store it or allow it to escape someway, unless you made a snapshot of it.
* The last char sequence may have less characters than the given [size].
*
* @param size the number of elements to take in each char sequence, must be positive and can be greater than the number of elements in this char sequence.
*
* @sample samples.text.Strings.chunkedTransformToSequence
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence<R> {
return windowedSequence(size, size, transform)