add pads and breakAsPairs extensions

This commit is contained in:
InsanusMokrassar 2024-10-24 15:30:10 +06:00
parent 54cc353bcc
commit 6245b36bdb
3 changed files with 49 additions and 0 deletions

View File

@ -2,6 +2,10 @@
## 0.22.8 ## 0.22.8
* `Common`:
* Add `List.breakAsPairs` extension
* Add `Sequence.padWith`/`Sequence.padStart`/`Sequence.padEnd` and `List.padWith`/`List.padStart`/`List.padEnd` extensions
## 0.22.7 ## 0.22.7
* `Versions`: * `Versions`:

View File

@ -0,0 +1,13 @@
package dev.inmo.micro_utils.common
fun <T> List<T>.breakAsPairs(): List<Pair<T, T>> {
val result = mutableListOf<Pair<T, T>>()
for (i in 0 until size - 1) {
val first = get(i)
val second = get(i + 1)
result.add(first to second)
}
return result
}

View File

@ -0,0 +1,32 @@
package dev.inmo.micro_utils.common
inline fun <T> Sequence<T>.padWith(size: Int, inserter: (Sequence<T>) -> Sequence<T>): Sequence<T> {
var result = this
while (result.count() < size) {
result = inserter(result)
}
return result
}
inline fun <T> Sequence<T>.padEnd(size: Int, padBlock: (Int) -> T): Sequence<T> = padWith(size) { it + padBlock(it.count()) }
inline fun <T> Sequence<T>.padEnd(size: Int, o: T) = padEnd(size) { o }
inline fun <T> List<T>.padWith(size: Int, inserter: (List<T>) -> List<T>): List<T> {
var result = this
while (result.size < size) {
result = inserter(result)
}
return result
}
inline fun <T> List<T>.padEnd(size: Int, padBlock: (Int) -> T): List<T> = asSequence().padEnd(size, padBlock).toList()
inline fun <T> List<T>.padEnd(size: Int, o: T): List<T> = asSequence().padEnd(size, o).toList()
inline fun <T> Sequence<T>.padStart(size: Int, padBlock: (Int) -> T): Sequence<T> = padWith(size) { sequenceOf(padBlock(it.count())) + it }
inline fun <T> Sequence<T>.padStart(size: Int, o: T) = padStart(size) { o }
inline fun <T> List<T>.padStart(size: Int, padBlock: (Int) -> T): List<T> = asSequence().padStart(size, padBlock).toList()
inline fun <T> List<T>.padStart(size: Int, o: T): List<T> = asSequence().padStart(size, o).toList()