Skip to content

Implement Enum.equal? #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2013
Merged

Implement Enum.equal? #978

merged 1 commit into from
Apr 27, 2013

Conversation

meh
Copy link
Contributor

@meh meh commented Apr 26, 2013

It compares different kinds of collections based on the elements it would iterate through.

In my case, it would be useful to compare ets tables of ordered set type, it would also be used as fallback in Dict.equal? which I'm going to implement now.

@josevalim
Copy link
Member

👍 We just need to discuss the naming. Dict.equal? should return true regardless of the order in the items. Enum can't and shouldn't do the same. So I am not sure if we should call it something else (what does clojure call it?) or keep it equal?.

@josevalim
Copy link
Member

Oh, the build failed. Could you please check?

@meh
Copy link
Contributor Author

meh commented Apr 26, 2013

Yeah, checking, I ran the tests and it was all green here.

@meh
Copy link
Contributor Author

meh commented Apr 26, 2013

Oh, I completely forgot about doctests, I have no idea about how it works, any hints?


iex> Enum.equal?([], [])
true
iex> Enum.equal?(1 .. 3, 1 .. 3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be it would make sense to add Enum.equal?(1 .. 3, [1 .. 3]) as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in the examples? It should return false, shouldn't it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant 1..3, [1,2,3] this way it'll compare if both enums are equal as enums.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sure thing, fixing it.

@meh
Copy link
Contributor Author

meh commented Apr 26, 2013

Yeah, doctests are magic, I forgot to add a case with empty list and non empty iterator and luckily I put it in the examples.

@meh
Copy link
Contributor Author

meh commented Apr 26, 2013

So I am not sure if we should call it something else (what does clojure call it?) or keep it equal?.

user=> (= { :a 2 :b 3 } { :b 3 :a 2 })
true

I guess Dict.equal? would be fine.

@josevalim
Copy link
Member

Yes, Dict.equal? will be fine. My worries are about Enum.equal?. The
question is: should we make a difference between semantic equality (as in
Dict.equal?) and structural equality (as in Enum.equal?). If so, what
should we call the latter?

José Valim
www.plataformatec.com.br
Founder and Lead Developer
*

@meh
Copy link
Contributor Author

meh commented Apr 26, 2013

user=> (= [1 2 3] '(1 2 3))
true

Clojure doesn't care.

@ericmj
Copy link
Member

ericmj commented Apr 26, 2013

@josevalim Do you mean difference in ordering of elements? Or difference in structure? (i think both Enum.equal? and Dict.equal? should ignore structure)

@josevalim
Copy link
Member

Yes, structural was not the best choice of word. I should have said
semantic or not, Enum is not structural. :)

Let me give another example: today we have Keyword.equal? that works only
for keywords. Would we want something like this too? The same as
Dict.equal? but also asserting on the type?

Another example of semantical behavior: String.equal?. We could have a
version that checks if two Unicode strings are the same, since a string
like "josé" can be represented in two different ways.

Note we already have a strict equal impl, via ==. :)

José Valim
www.plataformatec.com.br
Founder and Lead Developer
*

@meh
Copy link
Contributor Author

meh commented Apr 26, 2013

So like an Enum.equal?/3 with a first "type" check?

@meh
Copy link
Contributor Author

meh commented Apr 27, 2013

Wait on merging this if the naming issue is resolved, I'm adding a version where you can provide a function to compare the elements.

josevalim pushed a commit that referenced this pull request Apr 27, 2013
@josevalim josevalim merged commit 2e26d17 into elixir-lang:master Apr 27, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants