KeithFrost
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.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
I can not tell you why the decission was made like this, but:
Emphasizis mine.
josevalim
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.
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
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
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
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
The smaller tuple comes first.
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
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
If you are ok with a contrived example, I could define:
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.