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