gcx11
01/18/2020, 12:53 PMinline fun <T, C: MutableCollection<T>> MutableList<T>.retrieveAllTo(collection: C, predicate: (T) -> Boolean): C {
val iterator = this.listIterator()
for (item in iterator) {
if (predicate(item)) {
collection.add(item)
iterator.remove()
}
}
return collection
}
inline fun <T> MutableList<T>.retrieveAll(predicate: (T) -> Boolean): List<T> {
return mutableListOf<T>().also { retrieveAllTo(it, predicate) }
}
Dominaezzz
01/18/2020, 1:32 PMfilter
and then removeAll
. In general, there isn't a function to mutate a collection while simultaneously creating another.gsala
01/20/2020, 7:11 AMpartition
inline fun <T> Array<out T>.partition(
predicate: (T) -> Boolean
): Pair<List<T>, List<T>>
To get the original list and the resulting list in a pair