This commit is contained in:
alex-s168
2024-10-28 18:53:53 +01:00
parent 234a682f7e
commit f0b2736af5
18 changed files with 799 additions and 232 deletions

View File

@@ -30,4 +30,40 @@ fun <T> MutableList<T>.addFront(value: T) =
add(0, value)
fun <T: Any> Iterable<T?>.countNotNull() =
count { it != null }
count { it != null }
fun <T> Iterable<Iterable<T>>.intersections(dest: MutableList<T> = mutableListOf()): MutableList<T> =
reduce { acc, li -> acc.intersect(li) }
.forEach { dest += it }
.let { dest }
fun <T> Iterable<T>.removeAtIndexes(idc: Iterable<Int>, dest: MutableList<T> = mutableListOf()): MutableList<T> =
filterIndexedTo(dest) { index, _ -> index !in idc }
fun <T> List<T>.gather(idc: Iterable<Int>): MutableList<T> {
val dest = mutableListOf<T>()
idc.forEach {
dest += get(it)
}
return dest
}
fun <T> List<T>.before(idx: Int): List<T> =
take(idx)
fun <T> List<T>.after(idx: Int): List<T> =
drop(idx + 1)
inline fun <I, reified O> Collection<I>.mapToArray(fn: (I) -> O): Array<O> {
val iter = this.iterator()
return Array(this.size) {
fn(iter.next())
}
}
inline fun <I, reified O> Collection<I>.mapIndexedToArray(fn: (Int, I) -> O): Array<O> {
val iter = this.iterator()
return Array(this.size) {
fn(it, iter.next())
}
}