blackode

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 :slight_smile:

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Logic is exactly the right word! The answer here has to do with the relationship between the existential and universal quantifiers. Formally:

image

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.

21
Post #4

Also Liked

Eiji

Eiji

Those functions are really well documented.

For Enum.all?/2:

When an element has a falsy value ( false or nil ) iteration stops immediately and false is returned. In all other cases true is returned.

For Enum.any?/2:

When an element has a truthy value (neither false nor nil ) iteration stops immediately and true is returned. In all other cases false is 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.

  1. In Enum.all?/2 case we check if all elements passes check and since there are no elements it acts like all elements passes. Please look at it like that: we have 0 items and 0 items passed, so all items passed - or in another words there was no item which does not passed.

  2. In Enum.any?/2 case we check if any element passes check and since we have no element no element from list passes check, so we are getting false here. Please look at it like that: we have 0 items and 0 items 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

vlad.grb

If you open the source code here then you will find that it is a simple reduce operation staring with false as accumulative value. So when your list is empty then it gives your false. The same for all? it starts with true and empty list doesn’t trigger the iteration process.

adolfont

adolfont

As a Logic in Computing professor, it is great to see a direct application of what I teach!

Where Next?

Popular in Questions Top

mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47849 226
New

We're in Beta

About us Mission Statement