sodapopcan

sodapopcan

I’ve been in the Elixir world for five years—not super long but also not super short. In this time I have seen lots of code that uses elem/2 but not once have I ever seen a situation where it made code clearer and in fact has always made code less clear. It seems its main use it to “not break pipelines.” To put it lightly: this is a non-goal. I have never run into a situation where pattern matching isn’t way way way waaaaaaaay clearer than elem/2, even if it means breaking the pipe.

IMO if we ever see an Elixir 2.0 this function should be removed from the language. But please tell me why elem/2 is indispensable to your Elixir work! I realize I’m using very dismissive language but I’m genuinely curious if there is a good use-case for it I’ve never thought of.

Showing Posts 1 to 10

arcanemachine

arcanemachine

Sometimes I just want to write a one-liner or pipeline and I’m too lazy to get fancy with the error handling. :man_shrugging:

dorgan

dorgan

  • You have tuples, a fixed size array of elements.
  • You can access them by a random index.
  • There is no syntax to access a random index of a tuple
  • Pattern matching requires you to know the length of the tuple up-front

I think it makes sense to have such a function be part of the standard library. The reasoning is quite simple: if you provide a builtin data structure, it makes sense to provide an API to read and write to it.

Wether or not it’s idiomatic to access a random element of a tuple is a whole different topic.

15
Post #2
v0idpwn

v0idpwn

Agree 100% with doorgan.

With regards to elem/2 in pipelines, maybe a credo check for this would be useful.

heilong

heilong

elem/2 can be used in guards, pattern matching cannot

elem/2 is O(1) access to a vector (the only other O(1) example is binaries).

Pattern matching with large tuples is verbose and it’s easy to get the wrong element (why count wildcard matches when you can write the correct index into a function argument).

In the future, it seems we will get O(1) update of tuples in contexts where the compiler knows there are no shared references. So maybe time to incorporate :erlang.setelement/3 into Elixir somehow.

BartOtten

BartOtten

Pattern matching with large tuples is verbose and it’s easy to get the wrong element

If you are on control of the data: Use Records to overcome this issue

dimitarvp

dimitarvp

Agreed, but it has to be noted that then – which solves the problem of not breaking pipelines – was only added in Elixir 1.12.

Use then.

sodapopcan

sodapopcan OP

Thanks for all the responses?

Ya, what I’m really asking is: “Does anyone have an example of an exemplary use of elem/2?” I’ve never been in a situation where I don’t know the size of a tuple. I always think of the size as being part of the type as it in other functional languages. But thanks for the thoughtful answer, all that does make sense!

I actually wasn’t aware you could use elem in guards! I still think pattern matching is clearer.

The question here is why do you have large tuples? I don’t think I’ve come across one with more than 4 before, though I could be wrong. The type of work I do is very limited to business web apps, but large tuples seem like a massive anti-pattern to me.

I’m having trouble finding it but I remember the docs recommending against put_elem and to pattern match and create a new tuple instead. Though maybe it’s gone and no longer the advice anymore? Or I’m just not looking hard enough.

This interesting as I’m aware of but never used records before.

cevado

cevado

I particularly wouldn’t use records in elixir, I think the semantics are not particularly well ported. the only use I see for records in elixir is to interact with erlang existing records. I was even thinking to open an issue/suggest changes on that in the mailing list but didn’t had time… in this topic I showed a gist of it:
https://forum.elixirforum.com/t/any-reason-for-the-different-semantics-with-records-in-elixir/

but I guess i’m going too off-topic here :smile:

sodapopcan

sodapopcan OP

I don’t actually have a problem with it in any apps I work on, even at work, was just generally looking for a case where pattern matching isn’t better. Though it could actually be a fun but jarring addition to recode :sweat_smile:

sodapopcan

sodapopcan OP

Everyone is always free to go off topic in any thread I create :sweat_smile:

Where Next? Top

Trending in Questions Top

katta
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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
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
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews