mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-25 19:48:45 +00:00
add pads and breakAsPairs extensions
This commit is contained in:
parent
54cc353bcc
commit
6245b36bdb
@ -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`:
|
||||||
|
@ -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
|
||||||
|
}
|
@ -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()
|
Loading…
Reference in New Issue
Block a user