brescia123
04/16/2018, 8:26 AMpublic 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!orangy
brescia123
04/16/2018, 8:28 AMorangy
all
answers the following question: do all items in the collection match the predicate? the answer is yes, all (zero) items match.edwardwongtl
04/16/2018, 8:38 AMjereksel
04/16/2018, 9:02 AMall
returns false, I'm expecting to have element there that doesn't pass predicate. With empty list there is no such element.edwardwongtl
04/16/2018, 9:45 AMall
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.orangy
any { !predicate(it) }
is what you’re looking for thenjereksel
04/16/2018, 9:48 AMall
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.spand
04/16/2018, 9:48 AMall
is defined to return true on empty setskarelpeeters
04/16/2018, 9:57 AMkarelpeeters
04/16/2018, 10:23 AMall{f(it)} == !any{!f(it)}
and it's intuitive that any
should be false for empty lists.