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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #ai
- #graphql
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
arcanemachine
Sometimes I just want to write a one-liner or pipeline and I’m too lazy to get fancy with the error handling.
dorgan
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.
v0idpwn
Agree 100% with doorgan.
With regards to elem/2 in pipelines, maybe a credo check for this would be useful.
heilong
elem/2can be used in guards, pattern matching cannotelem/2is 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/3into Elixir somehow.BartOtten
If you are on control of the data: Use Records to overcome this issue
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
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
elemin 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_elemand 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
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
sodapopcan
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
sodapopcan
Everyone is always free to go off topic in any thread I create