There are times you just want to know if a certain element is in the collection, but don’t care what it is:
fun containsOdd(list: List<Int>) = list.firstOrNull { it % 2 == 1 } != null
But this looks kind of ugly with all the null business. I wish we had (in _Collections.kt) :
/**
* Returns `true` if an element matching the [predicate] is found in the collection.
*/
public inline fun <T> Iterable<T>.contains(predicate: (T) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return true
return false
}
(With similar for _Arrays.kt)
Then we could just do:
fun containsOdd(list: List<Int>) = list.contains { it % 2 == 1 }
any(p: (T) -> Boolean): Boolean is paired with all(p: (T) -> Boolean): Boolean.
They work like the mathematical quantifiers “there exists (∃)” and “for all (∀)”, respectively.
They are the same as exists(predicate) and forall(predicate) in Scala.
You now, all(element: T) does not make much sense, because it will be true only if all of the elements of the collection is the same.