Docs and samples for 'windowed' function

This commit is contained in:
Ilya Gorbunov
2017-05-18 08:33:13 +03:00
parent b2f2e3537b
commit 99bd54116e
11 changed files with 443 additions and 15 deletions
+29 -1
View File
@@ -1819,7 +1819,7 @@ public fun <T> Iterable<T>.chunked(size: Int): List<List<T>> {
* @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.
* 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].
*
* @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.
@@ -2034,6 +2034,19 @@ public inline fun <T> Collection<T>.plusElement(element: T): List<T> {
return plus(element)
}
/**
* Returns a list of snapshots of the window of the given [size]
* sliding along this collection with the given [step], where each
* snapshot is a list.
*
* Several last lists may have less elements than the given [size].
*
* 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
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>> {
checkWindowSizeStep(size, step)
@@ -2054,6 +2067,21 @@ public fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>> {
return result
}
/**
* Returns a list of results of applying the given [transform] function to
* an each list representing a view over the window of the given [size]
* sliding along this collection with the given [step].
*
* 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 to escape in some way, unless you made a snapshot of it.
* Several last lists may have less elements than the given [size].
*
* 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
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <T, R> Iterable<T>.windowed(size: Int, step: Int, transform: (List<T>) -> R): List<R> {
checkWindowSizeStep(size, step)
+29 -1
View File
@@ -1354,7 +1354,7 @@ public fun <T> Sequence<T>.chunked(size: Int): Sequence<List<T>> {
* @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.
* 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].
*
* @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.
@@ -1563,11 +1563,39 @@ public inline fun <T> Sequence<T>.plusElement(element: T): Sequence<T> {
return plus(element)
}
/**
* Returns a sequence of snapshots of the window of the given [size]
* sliding along this sequence with the given [step], where each
* snapshot is a list.
*
* Several last lists may have less elements than the given [size].
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this sequence.
* @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
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun <T> Sequence<T>.windowed(size: Int, step: Int): Sequence<List<T>> {
return windowedSequence(size, step, dropTrailing = false, reuseBuffer = false)
}
/**
* Returns a sequence of results of applying the given [transform] function to
* an each list representing a view over the window of the given [size]
* sliding along this sequence with the given [step].
*
* 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 to escape in some way, unless you made a snapshot of it.
* Several last lists may have less elements than the given [size].
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this sequence.
* @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
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <T, R> Sequence<T>.windowed(size: Int, step: Int, transform: (List<T>) -> R): Sequence<R> {
return windowedSequence(size, step, dropTrailing = false, reuseBuffer = true).map(transform)
+58 -2
View File
@@ -1131,7 +1131,7 @@ public fun CharSequence.chunked(size: Int): List<String> {
* @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.
* You should not store it or allow it to escape in some way, 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.
@@ -1164,7 +1164,7 @@ public fun CharSequence.chunkedSequence(size: Int): Sequence<String> {
* @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.
* You should not store it or allow it to escape in some way, 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.
@@ -1243,11 +1243,39 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, S
return Pair(first.toString(), second.toString())
}
/**
* Returns a list of snapshots of the window of the given [size]
* sliding along this char sequence with the given [step], where each
* snapshot is a string.
*
* Several last strings may have less characters than the given [size].
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @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
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun CharSequence.windowed(size: Int, step: Int): List<String> {
return windowed(size, step) { it.toString() }
}
/**
* Returns a list of results of applying the given [transform] function to
* an each char sequence representing a view over the window of the given [size]
* sliding along this char sequence with the given [step].
*
* 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 in some way, unless you made a snapshot of it.
* Several last char sequences may have less characters than the given [size].
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @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
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.windowed(size: Int, step: Int, transform: (CharSequence) -> R): List<R> {
checkWindowSizeStep(size, step)
@@ -1261,11 +1289,39 @@ public fun <R> CharSequence.windowed(size: Int, step: Int, transform: (CharSeque
return result
}
/**
* Returns a sequence of snapshots of the window of the given [size]
* sliding along this char sequence with the given [step], where each
* snapshot is a string.
*
* Several last strings may have less characters than the given [size].
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @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
*
* @sample samples.collections.Sequences.Transformations.takeWindows
*/
@SinceKotlin("1.2")
public fun CharSequence.windowedSequence(size: Int, step: Int): Sequence<String> {
return windowedSequence(size, step) { it.toString() }
}
/**
* Returns a sequence of results of applying the given [transform] function to
* an each char sequence representing a view over the window of the given [size]
* sliding along this char sequence with the given [step].
*
* 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 in some way, unless you made a snapshot of it.
* Several last char sequences may have less characters than the given [size].
*
* Both [size] and [step] must be positive and can be greater than the number of elements in this char sequence.
* @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
*
* @sample samples.collections.Sequences.Transformations.averageWindows
*/
@SinceKotlin("1.2")
public fun <R> CharSequence.windowedSequence(size: Int, step: Int, transform: (CharSequence) -> R): Sequence<R> {
checkWindowSizeStep(size, step)