Marcus

Marcus

Enum.sort(enumerable, module) - why not Enum.sort(enumerable, comparable)

Hello,

I have taken a look at Enum.sort and it is great to have the option to give the function a module to use the module.compare/2 function. But, wouldn’t it be nice if the module implemented a Comparable protocol?
In this case, Comparable would be a Protocol with just compare/2 as the only function.

What do you think?

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

As for your specific suggestion: An immediate problem is that we might want to compare two different datatypes with one-another. Now the question becomes: which protocol implementation to call?

Passing a module (and using compare as a “behaviour” function) is more flexible and handles these kinds of situations without problem.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @marcus there have been some proposals for a comparable protocol before on the mailing list: Redirecting to Google Groups

al2o3cr

al2o3cr

There’s a certain amount of overhead for protocol dispatch. I haven’t measured how much, but there’s certainly more work for the compiler.

Another reason (I suspect) is historical: a lot of data types had a compare function even before sort supported this option, so most of the protocol implementations would be trivial:

defimpl Comparable, for: DateTime do
  def compare(d1, d2), do: DateTime.compare(d1, d2)
end

But the subtlest reason is because protocol dispatch isn’t symmetric in its arguments if they aren’t the same type. For instance, if you have the following:

defimpl Comparable, for: A do
  def compare(a1, a2), do: ...
end

defimpl Comparable, for: B do
  def compare(b1, b2), do: ...
end

Then sorting a list of mixed A and B structs will dispatch calls like Comparable.compare(a, b) and Comparable.compare(b, a) - which lead to DIFFERENT functions based on the first argument.

Where Next?

Popular in Discussions Top

AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31707 143
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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 49134 226
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

We're in Beta

About us Mission Statement