natewallis
Hi,
I have recently started devoting some more time to learning Elixir and a question that always nags me is why do tuples exist in Elixir? Is it purely because they are a side effect of erlang and therefore had to be brought across?
Whether to use a tuple or a list feels like a bit of a grey area to me.
Where do people draw the line when it comes to deciding on whether to use a tuple or a list?
Thanks,
Nate
Trending in Discussions
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
Hey folks,
I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Hi everyone,
I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
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
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 just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
derek-zhou
Older functional language such as Lisp do not have tuples. So they are not strictly speaking necessary. However, they make certain ideas clearer, certain operations cheaper, so most modern languages, functional or not, tend to have it.
As for where to draw the line, just look at elixir’s core library. Ask yourself why a tuple is used instead of a list anywhere you see it. Eventually you will have the idea.
gregvaughn
Tuples and Lists are both compound data structures. Lists typically store multiple instances of the same type of thing. Tuples typically store different types of things. Tuples are lousy at enumerating through the elements. Lists are great at that.
natewallis
Ok, thanks.
I guess it is going to come down to how the community has decided that tuples should be used? For example in function responses and representing things like x,y coordinates. That is where I have seen them.
To me, it seems that if Tuples are not great at enumerating, then Lists have them covered.
mpope
Tuples have an advantage over lists because they’re stored contiguously. They have better ‘spatial locality’. Caching benefits from two types of locality, ‘temporal’ which is reusing a specific part of memory, or ‘spatial’ locality, which is the ability to load a segment of data into a CPU cache / RAM and have fast access to parts of that data because they’re close together. Tuples help with the later. Lists potentially require more random memory access.
Due to these characteristics, tuples are comparatively fast to read and create but slow to modify. More detailed information can be found here: Client Challenge
natewallis
Thanks!
dimitarvp
Tuples are like immutable arrays in other languages.
Additionally, they are used as a workaround to overcome the problem of functions not being able to return more than one value. With tuples you can encapsulate an arbitrary amount of return values.
eksperimental
I would say the main benefit is access time. Tuple are fast for reading but not for adding elements.
If the size of your data structure is not going to change, tuple is a good option. You can determine the size of a time in constant time, while with a list you need to traverse the whole list to find out how long it is.
aseigo
To pile in on this one
Tuples are: strictly positional (you won’t be moving their contents around, nor will you be referencing the contents by keys, and pattern matches must be exact), are not Enumerable (so you won’t get to use the
Enummodule), relatively space efficient (caveats apply) and fast for random access (due to positional nature). Various mutations are provided in theTuplemodule.Maps are: fast lookups, good update performance, values have lookup keys, they are Enumerable, and pattern matches are quite free-form due to the key’d values
Lists are: Enumerable, suited to collections of items, poor for random access at large sizes, but good for iterating and modifying. The swiss-army-knife data bag of Elixir, and often more than fast-enough in most cases.
Keyword lists are: Lists of 2-Tuples!
As Keyword lists show, these can all be mixed and matched: tuples as the values (or keys!) of maps, maps as the values in a list, lists as elements in tuples .. algebraic types ftw!
tcoopman
This blog from @sasajuric is also a great resource. Comparing some different data structures in different scenarios. Seems like tuples definitely have their place in some use cases
aseigo
Indeed, and not only for performance reasons. When one wants to ensure that the data is of a specific size, they are perfect.
Lists and maps can be of different sizes, though they can be checked for length (at a cost), while with a tuple ensuring an exact count of elements can be done with a simple pattern match.
Keywordis a neat example of this. As a list of tagged tuples, it guarantees that each entry has exactly 2 members, and that the entries appear in the order of creation (thanks to being in a list). This would be more awkward with either lists-only or a map, and given how Keyword lists tend to be used would likely be slower (well, I haven’t benchmarked that, but it wouldn’t surprise me in the least)The common
{:ok, data} | {:error, reason}pattern for function returns is another.