blackode

blackode

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:

Showing Posts 1 to 10

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.

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.

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 #3
blackode

blackode OP

Thanks for time you took to document here with well explanation.

I almost read your content enough times to digest the intention behind those functions.

Now I got confusion to use Enum.all? I have to check for list emptiness before applying. There is no problem with Enum.any? here but Enum.all? confused with the sounds it has. I thought Enum.all? must evaluate the condition function given to not falsy Since we don’t pass any value in the list, as the priority check, the functional condition is no way it gets executed.

At higher level of understanding I thought it should be false while coming to lower level it is more sense to be true after looking your points.

So, for Enum.all? an extra check has to performed for Non Empty list. Before its actual usage.

As an experience, I thought it to be reverse.

Thanks for Everything

blackode

blackode OP

Glad you took the time and effort to document this. Appreciate that.
It’s more explanatory and ideal to look into.

The relationship between the existential and universal quantifiers is self-explanatory for the logic behind the function.

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.

This explains the hidden logic.

But needs bigger mind.

Thank you :slight_smile:

ityonemo

ityonemo

Honestly in 90%, maybe more, of cases you shouldn’t need to check if the list is empty before doing Enum.all?.

I have never to my memory written a check for emptiness in front of any list.all function in 20 or so years of coding.

blackode

blackode OP

Yeah! I used to code it so unless I met with the strange behavior or business requirement.

No doubt that we learn from Experiences.

adolfont

adolfont

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

aenglisc

aenglisc

Another way to describe it is it that all is a series of conjunctions (and) whereas any is a series of disjunctions (or), and by De Morgan’s laws not (A and B) = not A or not B

frumos

frumos

should it be said like:

Please look at it like that: we have 0 items and 0 items passed failed, …

instead?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews