windowed function: add default parameters, drop or keep partial windows, KEEP-11
Make step default to 1. Add partialWindows boolean parameter defaulting to false to control whether to keep partial windows in the end.
This commit is contained in:
@@ -1799,7 +1799,7 @@ public fun <T : Any> List<T?>.requireNoNulls(): List<T> {
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <T> Iterable<T>.chunked(size: Int): List<List<T>> {
|
||||
return windowed(size, size)
|
||||
return windowed(size, size, partialWindows = true)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1818,7 +1818,7 @@ public fun <T> Iterable<T>.chunked(size: Int): List<List<T>> {
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <T, R> Iterable<T>.chunked(size: Int, transform: (List<T>) -> R): List<R> {
|
||||
return windowed(size, size, transform)
|
||||
return windowed(size, size, partialWindows = true, transform = transform)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2011,25 +2011,29 @@ public inline fun <T> Collection<T>.plusElement(element: T): List<T> {
|
||||
*
|
||||
* 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 step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @sample samples.collections.Sequences.Transformations.takeWindows
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>> {
|
||||
public fun <T> Iterable<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List<List<T>> {
|
||||
checkWindowSizeStep(size, step)
|
||||
if (this is RandomAccess && this is List) {
|
||||
val thisSize = this.size
|
||||
val result = ArrayList<List<T>>((thisSize + step - 1) / step)
|
||||
var index = 0
|
||||
while (index < thisSize) {
|
||||
result.add(List(size.coerceAtMost(thisSize - index)) { this[it + index] })
|
||||
val windowSize = size.coerceAtMost(thisSize - index)
|
||||
if (windowSize < size && !partialWindows) break
|
||||
result.add(List(windowSize) { this[it + index] })
|
||||
index += step
|
||||
}
|
||||
return result
|
||||
}
|
||||
val result = ArrayList<List<T>>()
|
||||
windowedIterator(iterator(), size, step, dropTrailing = false, reuseBuffer = false).forEach {
|
||||
windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = false).forEach {
|
||||
result.add(it)
|
||||
}
|
||||
return result
|
||||
@@ -2046,12 +2050,14 @@ public fun <T> Iterable<T>.windowed(size: Int, step: Int): List<List<T>> {
|
||||
*
|
||||
* 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 step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @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> {
|
||||
public fun <T, R> Iterable<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List<T>) -> R): List<R> {
|
||||
checkWindowSizeStep(size, step)
|
||||
if (this is RandomAccess && this is List) {
|
||||
val thisSize = this.size
|
||||
@@ -2060,13 +2066,14 @@ public fun <T, R> Iterable<T>.windowed(size: Int, step: Int, transform: (List<T>
|
||||
var index = 0
|
||||
while (index < thisSize) {
|
||||
window.move(index, (index + size).coerceAtMost(thisSize))
|
||||
if (!partialWindows && window.size < size) break
|
||||
result.add(transform(window))
|
||||
index += step
|
||||
}
|
||||
return result
|
||||
}
|
||||
val result = ArrayList<R>()
|
||||
windowedIterator(iterator(), size, step, dropTrailing = false, reuseBuffer = true).forEach {
|
||||
windowedIterator(iterator(), size, step, partialWindows, reuseBuffer = true).forEach {
|
||||
result.add(transform(it))
|
||||
}
|
||||
return result
|
||||
|
||||
@@ -1322,7 +1322,7 @@ public fun <T : Any> Sequence<T?>.requireNoNulls(): Sequence<T> {
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <T> Sequence<T>.chunked(size: Int): Sequence<List<T>> {
|
||||
return windowed(size, size)
|
||||
return windowed(size, size, partialWindows = true)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1343,7 +1343,7 @@ public fun <T> Sequence<T>.chunked(size: Int): Sequence<List<T>> {
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <T, R> Sequence<T>.chunked(size: Int, transform: (List<T>) -> R): Sequence<R> {
|
||||
return windowed(size, size, transform)
|
||||
return windowed(size, size, partialWindows = true, transform = transform)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1524,13 +1524,15 @@ public inline fun <T> Sequence<T>.plusElement(element: T): Sequence<T> {
|
||||
*
|
||||
* 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
|
||||
* @param step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @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)
|
||||
public fun <T> Sequence<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence<List<T>> {
|
||||
return windowedSequence(size, step, partialWindows, reuseBuffer = false)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1544,13 +1546,15 @@ public fun <T> Sequence<T>.windowed(size: Int, step: Int): Sequence<List<T>> {
|
||||
*
|
||||
* 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
|
||||
* @param step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @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)
|
||||
public fun <T, R> Sequence<T>.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List<T>) -> R): Sequence<R> {
|
||||
return windowedSequence(size, step, partialWindows, reuseBuffer = true).map(transform)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1113,7 +1113,7 @@ public inline fun CharSequence.sumByDouble(selector: (Char) -> Double): Double {
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun CharSequence.chunked(size: Int): List<String> {
|
||||
return windowed(size, size)
|
||||
return windowed(size, size, partialWindows = true)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1132,7 +1132,7 @@ public fun CharSequence.chunked(size: Int): List<String> {
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <R> CharSequence.chunked(size: Int, transform: (CharSequence) -> R): List<R> {
|
||||
return windowed(size, size, transform)
|
||||
return windowed(size, size, partialWindows = true, transform = transform)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1165,7 +1165,7 @@ public fun CharSequence.chunkedSequence(size: Int): Sequence<String> {
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <R> CharSequence.chunkedSequence(size: Int, transform: (CharSequence) -> R): Sequence<R> {
|
||||
return windowedSequence(size, size, transform)
|
||||
return windowedSequence(size, size, partialWindows = true, transform = transform)
|
||||
}
|
||||
|
||||
@Deprecated("Use zipWithNext instead", ReplaceWith("zipWithNext()"))
|
||||
@@ -1225,13 +1225,15 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, S
|
||||
*
|
||||
* 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
|
||||
* @param step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @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() }
|
||||
public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false): List<String> {
|
||||
return windowed(size, step, partialWindows) { it.toString() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1245,18 +1247,22 @@ public fun CharSequence.windowed(size: Int, step: Int): List<String> {
|
||||
*
|
||||
* 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
|
||||
* @param step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @sample samples.collections.Sequences.Transformations.averageWindows
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <R> CharSequence.windowed(size: Int, step: Int, transform: (CharSequence) -> R): List<R> {
|
||||
public fun <R> CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): List<R> {
|
||||
checkWindowSizeStep(size, step)
|
||||
val thisSize = this.length
|
||||
val result = ArrayList<R>((thisSize + step - 1) / step)
|
||||
var index = 0
|
||||
while (index < thisSize) {
|
||||
result.add(transform(subSequence(index, (index + size).coerceAtMost(thisSize))))
|
||||
val end = index + size
|
||||
val coercedEnd = if (end > thisSize) { if (partialWindows) thisSize else break } else end
|
||||
result.add(transform(subSequence(index, coercedEnd)))
|
||||
index += step
|
||||
}
|
||||
return result
|
||||
@@ -1271,13 +1277,15 @@ public fun <R> CharSequence.windowed(size: Int, step: Int, transform: (CharSeque
|
||||
*
|
||||
* 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
|
||||
* @param step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @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() }
|
||||
public fun CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false): Sequence<String> {
|
||||
return windowedSequence(size, step, partialWindows) { it.toString() }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1291,14 +1299,17 @@ public fun CharSequence.windowedSequence(size: Int, step: Int): Sequence<String>
|
||||
*
|
||||
* 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
|
||||
* @param step the number of elements to move the window forward by on an each step, by default 1
|
||||
* @param partialWindows controls whether or not to keep partial windows in the end if any,
|
||||
* by default `false` which means partial windows won't be preserved
|
||||
*
|
||||
* @sample samples.collections.Sequences.Transformations.averageWindows
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public fun <R> CharSequence.windowedSequence(size: Int, step: Int, transform: (CharSequence) -> R): Sequence<R> {
|
||||
public fun <R> CharSequence.windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): Sequence<R> {
|
||||
checkWindowSizeStep(size, step)
|
||||
return (indices step step).asSequence().map { index -> transform(subSequence(index, (index + size).coerceAtMost(length))) }
|
||||
val windows = (if (partialWindows) indices else 0 until length - size + 1) step step
|
||||
return windows.asSequence().map { index -> transform(subSequence(index, (index + size).coerceAtMost(length))) }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user