Lukasz Kalnik
08/30/2022, 2:38 PMIterable.all
returning true
when called on an empty List:
val list = emptyList<Boolean>()
list.all { it } // true
list.none { it } // true
list.any {it } // false
Klitos Kyriacou
08/30/2022, 2:40 PMLukasz Kalnik
08/30/2022, 2:43 PMnone
in the same way 😉Vampire
08/30/2022, 2:43 PMtrue
?
All elements in the list are satisfying the predicate.Lukasz Kalnik
08/30/2022, 2:44 PMnone
and any
with lambda argument are consistent with the no-argument versions:
list.none() == true
list.any() == false
because they check if the list is empty/not empty.Lukasz Kalnik
08/30/2022, 2:45 PMLukasz Kalnik
08/30/2022, 2:45 PMfun <T> none(predicate: T -> Boolean)
Landry Norris
08/30/2022, 2:46 PMKlitos Kyriacou
08/30/2022, 2:46 PMLukasz Kalnik
08/30/2022, 2:46 PMLukasz Kalnik
08/30/2022, 2:46 PMall
.Lukasz Kalnik
08/30/2022, 2:47 PMVampire
08/30/2022, 2:47 PMKlitos Kyriacou
08/30/2022, 2:49 PMall
to return false for empty lists, whatever programming language you use, you would have to have special treatment for empty lists. The logical way is to return true, because you iterate the list and return false if you find an element that doesn't satisfy the predicate; if you reach the end of the iteration, you return true. This is the case in most languages, not just Kotlin.Lukasz Kalnik
08/30/2022, 2:50 PMLukasz Kalnik
08/30/2022, 2:51 PMall
rather to check if there is at least one element that doesn't fulfill the predicate.Lukasz Kalnik
08/30/2022, 2:51 PMShawn
08/30/2022, 3:56 PM.all()
docstring https://en.wikipedia.org/wiki/Vacuous_truthJoffrey
08/30/2022, 3:56 PMLukasz Kalnik
08/30/2022, 4:19 PMephemient
08/30/2022, 5:23 PM(list + x).any() == list.any() || x
(list + x).all() == list.all() && x
(list + x).none() == list.none() && !x
these identities work for lists of all sizes; if you set list = emptyList()
and try different values of x
, it is clear there is only one reasonable value for those functions on an empty list