Hello. I'm in doubt about on bevahior from all met...
# server
e
Hello. I'm in doubt about on bevahior from all method on Collections:
Copy code
/**
 * Returns `true` if all elements match the given [predicate].
 * 
 * @sample samples.collections.Collections.Aggregates.all
 */
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
    if (this is Collection && isEmpty()) return true
    for (element in this) if (!predicate(element)) return false
    return true
}
Does this condition:
if (this is Collection && isEmpty()) return true
makes sense? Should return true for applying a predicate to an empty collection? Example:
Copy code
val s = emptyList<String>()
if (s.all { it.isNotEmpty() }) {
    // should  the code  enter here?
}
g
yes, it makes sense. all always return true for empty collection
e
👍
Yeah, I miss the basics here. All languages have the same behavior for this function.
m
all
===
noneDont
s
If you’re curious about how this works out from the set theory point of view: https://en.wikipedia.org/wiki/Vacuous_truth
👍 1
s
the first line seems like overhead to me. is it even faster in the case it's defined for? Is an if with two conditions really faster than iterating an empty iterator??
e
Really nice @Shawn!
If here is Stackoverflow I will give a +1 🙂
👍 1