blackode
Why Enum.all?([]) is true and Enum.any?([]) is false?
In General if the Enum.all? evaluates true that means all the elements in the given collection evaluates true for the given callback function applied.
If Enum.all? is true, then no doubt Enum.any? must also to be true and that makes sense.
But, Coming to the empty list [] this case has been reversed.
The following lines can give you the more idea. Could someone explain the logic behind this?
iex> Enum.all?([], &is_nil/1)
true
iex> Enum.any?([], &is_nil/1)
false
iex> Enum.all?([], & &1==143)
true
iex> Enum.any?([], & &1==143)
false
iex> Enum.all?([2,3,4], & &1>1)
true
iex> Enum.any?([2,3,4], & &1>1)
true
How the Enum Protocol behaves for an empty List []?
Just yesterday I got the businness requirement to use the Enum.all? to check for price value should meet the certain condition.
I simply code it like Enum.all?(list, & &1.price >= 143) to my surprise whenever the list is empty it is sending me true . It should send me as false right and that’s what I expected. Then I looked the docs for it.
Please correct me if I am wrong here. Glad if you could share intentions behind this function usage.
Thank You 
Marked As Solved
benwilson512
Logic is exactly the right word! The answer here has to do with the relationship between the existential and universal quantifiers. Formally:

In regular language, it’s saying that if for all x some proposition P(x) is true, then it’s false that there exists any x such that P(x) is false. Enum.all? is equivalent to the universal quantifier, and Enum.any? is equivalent to the existential quantifier. Let’s play with it!
iex(3)> Enum.all?([2,4,6], fn x -> Integer.is_even(x) end)
true
iex(4)> !Enum.any?([2,4,6], fn x -> !Integer.is_even(x) end)
true
This makes sense. If all of the integers are even, then obviously not any of them are not even. The high level relationship to keep in mind here: Enum.all?() == !Enum.any?(). If one is true, the other must be false.
Let’s focus on the Enum.any?([2,4,6], fn x -> !Integer.is_even(x) end) bit. The idea here is that we’re looking for any item that passes our test. If no item passes the test, we should return false.
iex(5)> Enum.any?([], fn x -> !Integer.is_even(x) end)
false
If we give it an empty list, then no possible item can pass the test, so it returns false. Our high level relationship from earlier told us that Enum.all?() == !Enum.any?(). Therefore if Enum.any? returns false with an empty list, then Enum.all? must return true for an empty list.
Also Liked
Eiji
Those functions are really well documented.
For Enum.all?/2:
When an element has a falsy value (
falseornil) iteration stops immediately andfalseis returned. In all other casestrueis returned.
For Enum.any?/2:
When an element has a truthy value (neither
falsenornil) iteration stops immediately andtrueis returned. In all other casesfalseis returned.
As you can see in both functions documentation says what happens in “all other cases”.
Maybe it could be seen as confusing, but when you think more about it then it makes more sense.
-
In
Enum.all?/2case we check ifallelements passes check and since there are no elements it acts like all elements passes. Please look at it like that: we have0items and0items passed, so all items passed - or in another words there was no item which does not passed. -
In
Enum.any?/2case we check ifanyelement passes check and since we have no element no element from list passes check, so we are gettingfalsehere. Please look at it like that: we have0items and0items passed, so no items passed - or in another words there was no item which have passed.
You can notice all items passed vs no items passed parts and that’s correct since we have all? vs any? naming.
vlad.grb
adolfont
As a Logic in Computing professor, it is great to see a direct application of what I teach!







