diff --git a/CHANGELOG.md b/CHANGELOG.md index 032047c5da6..045d5ef16d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 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 * `Versions`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/BreakAsPairs.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/BreakAsPairs.kt new file mode 100644 index 00000000000..d1e262e6188 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/BreakAsPairs.kt @@ -0,0 +1,13 @@ +package dev.inmo.micro_utils.common + +fun List.breakAsPairs(): List> { + val result = mutableListOf>() + + for (i in 0 until size - 1) { + val first = get(i) + val second = get(i + 1) + result.add(first to second) + } + + return result +} diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PadExtensions.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PadExtensions.kt new file mode 100644 index 00000000000..2bc5621d2ae --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PadExtensions.kt @@ -0,0 +1,32 @@ +package dev.inmo.micro_utils.common + +inline fun Sequence.padWith(size: Int, inserter: (Sequence) -> Sequence): Sequence { + var result = this + while (result.count() < size) { + result = inserter(result) + } + return result +} + +inline fun Sequence.padEnd(size: Int, padBlock: (Int) -> T): Sequence = padWith(size) { it + padBlock(it.count()) } + +inline fun Sequence.padEnd(size: Int, o: T) = padEnd(size) { o } + +inline fun List.padWith(size: Int, inserter: (List) -> List): List { + var result = this + while (result.size < size) { + result = inserter(result) + } + return result +} +inline fun List.padEnd(size: Int, padBlock: (Int) -> T): List = asSequence().padEnd(size, padBlock).toList() + +inline fun List.padEnd(size: Int, o: T): List = asSequence().padEnd(size, o).toList() + +inline fun Sequence.padStart(size: Int, padBlock: (Int) -> T): Sequence = padWith(size) { sequenceOf(padBlock(it.count())) + it } + +inline fun Sequence.padStart(size: Int, o: T) = padStart(size) { o } + +inline fun List.padStart(size: Int, padBlock: (Int) -> T): List = asSequence().padStart(size, padBlock).toList() + +inline fun List.padStart(size: Int, o: T): List = asSequence().padStart(size, o).toList()