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
- #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
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachallaun
An example from a project I contribute to:
Lexical.Document.LinesThe key insight is that a tuple provides O(1) access to any of its elements, whereas a list is O(n). So if you need a data structure that provides fast access to any arbitrary line of a document, you reach for a tuple. (And then you use
elem/2for access, since you don’t know what its length will be.)Also Liked
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.
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
Everyone is always free to go off topic in any thread I create
Last Post!
slouchpie
That is weird. I have the same behavior. I can not let this go. Why does
:pubnot “tab out” to:public? There must be a reason.Update: I think I got it
Any module that is Application “kernel” or “stdlib” or “erts” will tab out.
Otherwise it will not.
:public_keyis in its own “public_key” application.Similarly,
:pretty_pr,:os_supand:release_handlerwill not tab out.