Add Enum.every?

90 views
Skip to first unread message

Randson

unread,
Jan 21, 2021, 1:39:41 AM1/21/21
to elixir-lang-core
Hi all, recently I opened I pull request on Elixir repo. Which is:


Adding the Enum.every? function on the Enum module.

I had case where, given a list of atoms, all of the needs to return true:

Enum.every?([:some, :some, :some], fn x -> x == :some end)
...> true

Enum.every?([])
...> false

I tried with functions like Enum.all?/2 and Enum.any?/2. But the cases are follow:

Enum.all?/2 - Returns true in case of an empty list;
Enum.any?/2 - Returns false, which is correct but if there is a different value on the list. Probably this will return true.

So I took the name every because I'm used to use like this on JS. But as Valim mentioned on the PR, it could be all_non_empty? or something else.

What do you guys think?

Anil Kulkarni

unread,
Jan 21, 2021, 2:04:45 AM1/21/21
to elixir-l...@googlegroups.com
Given that the functionality is very similar to Enum.all?/2, I would suggest one of the following:

1) Solve this outside of the core elixir library with something like
case myList do
[] -> false
myList -> Enum.all?(myList, &(&1 == :some))
end

or

2) add a default parameter to Enum.all? to specify the default value when the list is empty.
E.g.
defmodule Enum do
def all?(list, fun \\ fn x -> x end, default \\ true) do
...
end
end

Regards,
Anil
> --
> You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/977fcb5e-3d81-4017-b993-7d27a695937an%40googlegroups.com.


Xavier Noria

unread,
Jan 21, 2021, 2:10:52 AM1/21/21
to elixir-l...@googlegroups.com
These functions return what they should. It's standard mathematical logic.

Enum.all? is a universal quantifier, asking if all members of a collection satisfy such condition. That is false if there is at least one member which does not satisfy the condition, otherwise it is true. In particular, it is true on an empty collection. "All elements of an empty set are prime" is a true statement in mathematics.

Enum.every? should be an alias to all?, they are similar concepts, should behave the same as a predicate.

You really need your own function "collection is not empty and all?".

eksperimental

unread,
Jan 21, 2021, 7:45:57 AM1/21/21
to elixir-l...@googlegroups.com
This version would be more idiomatic:

`not Enum.empty?(enumerable) and Enum.all?(enumerable, &(&1 == :some))`

If were were to choose a name for the new functions I would go with `Enum.every_one?(enumerable, fun)`

Randson

unread,
Jan 21, 2021, 7:47:40 AM1/21/21
to elixir-lang-core
Xavier

Yes. I got a case where the Enum.all?/2 doesn't work so I need to do what Anil told.

I put a name every because I want to ask if every member on the list satisfy such a condition. That is false if there is at least one member which does not satisfy the condition, otherwise it is false. An empty list in this case should be false.

Felipe Stival

unread,
Jan 21, 2021, 7:51:28 AM1/21/21
to elixir-l...@googlegroups.com
Personally, I don't like having both `every?` and `all?`, I think it would be ambiguous. And I think the use-case does not justify this ambiguity. 

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Xavier Noria

unread,
Jan 21, 2021, 7:55:03 AM1/21/21
to elixir-l...@googlegroups.com
On Thu, Jan 21, 2021 at 1:47 PM Randson <oran...@gmail.com> wrote:

I put a name every because I want to ask if every member on the list satisfy such a condition.

Yes, I understand. But it would be confusing, every? and all? are synonyms. They are both universal quantifiers, and both should return true for empty collections.

So, you really need something else added to the name of the function, or add an option to all?.

Personally, I do not find a good name for this, and do not see it in Enum as such, the idiomatic test is not empty? and all?

Also, personally do not quite see an option in all?, because all? is all?, all? AND not-empty is like mixing two unrelated things in one call, to me.


Jon Rowe

unread,
Jan 21, 2021, 7:57:12 AM1/21/21
to elixir-l...@googlegroups.com
I think this is too confusing compared to `all?`, the words are synonyms but I agree that having `all?` return `true` for an empty list can create extra cruft unless you create your own wrapper functions, personally I wouldn’t mind an option to configure it with a default to true, such as `Enum.all?(enum, fnx, on_empty: false)`

Randson

unread,
Jan 21, 2021, 8:00:10 AM1/21/21
to elixir-l...@googlegroups.com
Yeah, I agree with you guys. The words are synonyms and I think we can add an option to all?/2, where default for breaking changes is false and if you really need that empty lists should return false. If specify it on an third argument.

I could prepare a PR this week implementing that.

What do you guys think? 
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

eksperimental

unread,
Jan 21, 2021, 8:07:20 AM1/21/21
to elixir-l...@googlegroups.com

José Valim

unread,
Jan 21, 2021, 8:08:49 AM1/21/21
to elixir-l...@googlegroups.com
Hi Randson,

Is there an argument against using "not Enum.empty?(coll) and Enum.all?(coll)", which would be by far the most readable implementation of what is being proposed?

I would like to see the cons to the suggestion above that justify the benefits of adding a default argument or an option to the standard library.

Thank you.

Randson

unread,
Jan 21, 2021, 8:27:40 AM1/21/21
to elixir-l...@googlegroups.com
Is there an argument against using "not Enum.empty?(coll) and Enum.all?(coll)"

Nothing. When I came to this. I thought it would be good to having a function to return false when a list is empty and the probably the same implementation of all?/2, because in my personally case the all?/2 returning true for empty lists doesn’t worked.

So, I searched on the Enum module to find this function and didn’t found nothing related. That’s why we’re here.

Well, I’ll use this way from now on. Thanks for the discussion and your time guys.

Xavier Noria

unread,
Jan 21, 2021, 8:33:09 AM1/21/21
to elixir-l...@googlegroups.com
To me, this is like adding an option to any? Let's imagine: any?(enum, on_empty: true).

You'd say, man, how can you special-case the enum like that, the predicate is about finding some element! If there is no element, then, the result should be false. If you need contextual logic about the emptiness of the collection, write it next to the predicate.

Same for all?(enum, on_empty: false). How can all? return false for an empty collection? It does not make sense! If there are no elements, all? should be true! It is the same argument argued above for any? It's equally off for me.

In my view, if you need logic about the emptiness, you write it apart, do not mix it.

Bruce Tate

unread,
Jan 21, 2021, 8:34:54 AM1/21/21
to elixir-l...@googlegroups.com
-1 from me on the .every? function, and the option. Go with mathematical logic in this case. 

-bt

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.


--

Regards,
Bruce Tate
CEO

Mário Guimarães

unread,
Jan 21, 2021, 9:28:15 AM1/21/21
to elixir-l...@googlegroups.com
I totally agree with Xavier. 

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAM%3DYcdi_Hb2%2BwDWgLuC_Dqe7XwgaiuBVzC%2BSXC07wkuGJwy9ZQ%40mail.gmail.com.

eksperimental

unread,
Jan 21, 2021, 9:31:59 AM1/21/21
to elixir-l...@googlegroups.com
Adding Xavier's explanation to the documentation would be of great
help, and easier to spot if they are contained in its own section.
> > <https://groups.google.com/d/msgid/elixir-lang-core/CAM%3DYcdi_Hb2%2BwDWgLuC_Dqe7XwgaiuBVzC%2BSXC07wkuGJwy9ZQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> > .
> >
>

Xavier Noria

unread,
Jan 21, 2021, 10:53:38 AM1/21/21
to elixir-l...@googlegroups.com
On Thu, Jan 21, 2021 at 3:31 PM eksperimental <eksper...@autistici.org> wrote:

Adding Xavier's explanation to the documentation would be of great
help, and easier to spot if they are contained in its own section.

Makes sense, it is an edge case that may surprise if you've never seen this logic before. I'll volunteer a doc patch. 

Randson

unread,
Jan 21, 2021, 4:55:16 PM1/21/21
to elixir-l...@googlegroups.com
Great Xavier, your answer shows what I think about returning true when a list is empty. 

I'm pretty sure the name could be a better one. However, I think we could have a function for these cases and not only a parameter or something else. 

That's why I came with every?/2, if you guys agree with us we can have a better name for this ocasion. 

-
Randson

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Xavier Noria

unread,
Jan 22, 2021, 4:58:06 AM1/22/21
to elixir-l...@googlegroups.com

Carsten Bormann

unread,
Jan 22, 2021, 12:26:07 PM1/22/21
to elixir-l...@googlegroups.com
On 2021-01-21, at 22:55, Randson <oran...@gmail.com> wrote:
>
> That's why I came with every?/2, if you guys agree with us we can have a better name for this ocasion.

any_and_all?/2

(If you must have one.)

Grüße, Carsten

Reply all
Reply to author
Forward
0 new messages