KeithFrost

KeithFrost

Why are tuples ordered differently than lists in elixir/erlang?

Enum.sort([[3, -999], [3], [2, 999]])
# => [[2, 999], [3], [3, -999]]
Enum.sort([{3, -999}, {3}, {2, 999}])
# => [{3}, {2, 999}, {3, -999}]

I ask because it came up when I was storing directed graph edges in :gb_sets as {node1, node2}, and I couldn’t just collect all the edges from a node starting with

:gb_sets.iterator_from({node1}, set)

I had to write

:gb_sets.iterator_from({node1, @min_value}, set)

instead.

First 10 of 13 Posts Switch mode

NobbZ

NobbZ

I can not tell you why the decission was made like this, but:

Lists are compared element by element. Tuples are ordered by size, two tuples with the same size are compared element by element.

Emphasizis mine.

josevalim

josevalim

Creator of Elixir

Tuples and maps can get their size computed in constant time, so it is cheap to use that for sorting, that’s not the case for lists! And the sooner you can tell two elements are different, for example by checking their size, the faster sorting will be.

15
Post #2
KeithFrost

KeithFrost

Aha, the performance explanation makes sense, thanks @josevalim.
I think for lists, even in a crazy world where the implementation was modified to get length in constant time, the existing ordering would be needed, at least as an option, to preserve lexicographic ordering for charlists, among other applications.

garrison

garrison

Lexicographic sort behavior for tuples is very useful for building database indexes with nested terms.

On the other hand, I have yet to think of a good use for tuples sorted by size. The performance benefit doesn’t really matter if the behavior is useless. It’s actually worse, because in order to get the useful behavior you have to use lists, which will thrash the cache lines more than (contiguous) tuples.

IMO the tuple sort behavior is a design mistake. If for some reason you actually wanted to sort by size it would be trivial to prefix the tuples ({3, a, b, c}), but it’s much harder to go the other way.

rvirding

rvirding

Creator of Erlang

If you are not going to sort tuples by size how would you order the 2 tuples {x,39,d,z} and {x,39,d}? Yes, you can compare the elements but what do you do when one tuple “runs out” of elements? Does the smaller tuple then come first, that is no element is always smaller than any element? Or what?

The current method is simple and consistent.

jstimps

jstimps

Much of the usefulness of the term order is derived from the fact that it exists, not necessarily the internal semantics of the ordering (e.g. :lists.usort/1).

I appreciate the existing design because as a user I expect operations on tuples to be O(1) where possible.

If you say (a) a term ordering must exist, (b) tuple operations should be fast, and (c) list operations may be comparatively slower, then comparing the size of the tuples as a short circuit is the correct design. I believe this leads to fewer surprises to the user overall.

garrison

garrison

But if the tuples are the same size, the comparison is O(n) anyway. Is this not the common case? How often are you sorting tuples of different sizes? If anything short-circuiting is less predictable.

Like, with respect, because the list of people I’m disagreeing with here is pretty ridiculous, I find this argument kinda specious.

By this logic why not compare binaries by size first? You know why: it would be confusing and annoying. And with tuples it is also confusing and annoying, for the same reasons.

Schultzer

Schultzer

Yikes, if sorting tuple is O(n) then you are indeed right, I have yet to see anyplace where it is common to sort different size tuples, in fact it would not be a good idea to use tuples in generel if they are different sizes, as tuples are expensive to create in the first place.

The only place I could imagine the use would be in mnesia, ets and dets. But that feels like a red herring as you tend to use records for a table where the size is fixed and known at compile time, so in the end this only leaves the compiler, but I’ve even bigger doubts that a list of tuples are sorted there.

There could be an argument for persistent term, but that an extremely weak argument, an properly so rarely used that it you give you any measurable differences.

jstimps

jstimps

If you are ok with a contrived example, I could define:

-type vec2() :: {float(), float()}.
-type vec3() :: {float(), float(), float()}.

And have the useful property that vec2() < vec3(). What kind of program needs this? Not sure, but it’s a useful property nonetheless. I don’t think that lexicographic ordering is always superior.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement