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

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
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
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

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
KristerV
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
mudasobwa
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews