Hi <@U0AAQKT9Q> <@U092308M7>, I was reasoning abou...
# announcements
b
Hi @ilya.gorbunov @orangy, I was reasoning about the semantic meanining of the stdlib’s
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean
with my colleagues and I realized that a lot of them have different intuitive expectations about the output of the function when called on an empty list. Can you provide some insights about the decision of make it return
true
if the list is empty? Thanks!
o
Please don’t mention people if you didn’t have previous conversation with them on the topic. Also, #C0B8Q383C is a better place to ask such questions.
b
I’m sorry!
o
The reasoning is pretty simple.
all
answers the following question: do all items in the collection match the predicate? the answer is yes, all (zero) items match.
e
But if there is nothing is the list, then shouldn't it be nothing matches, which should return false?
j
Frankly, If
all
returns false, I'm expecting to have element there that doesn't pass predicate. With empty list there is no such element.
1
e
I think it depends on how you interpret it. If all element passes the predicate,
all
return true. Then emptyList should return false since no element passes. If treating it as no element fails the predicate, then emptyList should return true.
o
any { !predicate(it) }
is what you’re looking for then
j
"If all element passes the predicate,
all
return true. Then emptyList should return false since no element passes." - I think that's where the confuction is. The fact that no element passes is fine, because list has no element. The question is not if there is element in list that passes.
s
I would say the argument works both ways also. The better argument would be that everywhere else
all
is defined to return true on empty sets
k
Another agument, you probably want
all{f(it)} == !any{!f(it)}
and it's intuitive that
any
should be false for empty lists.