![JAR search and dependency download from the Maven repository](/logo.png)
ru.sadv1r.vk.parser.BatchingSequence.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vk-parser Show documentation
Show all versions of vk-parser Show documentation
vk.com API implementation
The newest version!
package ru.sadv1r.vk.parser
/**
* Функция для деления [последовательности][kotlin.sequences.Sequence] Sequence на части нужного размера.
*
* В Kotlin [планируется](https://github.com/JetBrains/kotlin/compare/rr/cy/window-sliding) добавление
* данной функциональности.
* [Предложение](https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/window-sliding.md) о добавлении.
*
* @author [sadv1r](http://sadv1r.ru)
*/
fun Sequence.batch(n: Int): Sequence> {
return BatchingSequence(this, n)
}
private class BatchingSequence(val source: Sequence, val batchSize: Int) : Sequence> {
override fun iterator(): Iterator> = object : AbstractIterator>() {
val iterate = if (batchSize > 0) source.iterator() else emptyList().iterator()
override fun computeNext() {
if (iterate.hasNext()) setNext(iterate.asSequence().take(batchSize).toList())
else done()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy