com.abelhu.utils._Collections.kt Maven / Gradle / Ivy
package com.abelhu.utils
/**
* Returns a list containing all elements satisfying the given [predicate]
*/
public inline fun Iterable.takeAll(predicate: (T) -> Boolean): List {
val list = ArrayList()
for (item in this) {
if (!predicate(item))
continue
list.add(item)
}
return list
}
/**
* Returns one element satisfying the given [predicate]
*/
public inline fun Iterable.takeOnly(predicate: (T) -> Boolean): T? {
forEach { item -> if (predicate(item)) return item }
return null
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy