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










Showing Posts 25 to 16- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.sodapopcan
No that’s what I mean, but there seems to be a possible autocomplete bug in iex. Typing
:us<tab>will autocomplete:user_but typing:pub<tab>does nothing. However, if I type out:public_key.in full then hit tab, it shows me the functions. And only after typing the module in full it will start to auto complete. In any event,:pub<tab>was all I tried last night which made me think it wasn’t in stdlibD4no0
:public_keypowers things like:ssl.You always know the length of a tuple, but you don’t want necessarily to match on the tuple, simply because this becomes horrible once the tuples are huge and nested, so this is where you ideally use records or
elem/2.cmo
One of the caching libs, cachex or nebulex, uses records. For performance reasons I presume.
sodapopcan
Thanks for more examples!
:public_keydoesn’t appear to be in the standard lib but I take it:public_key.pem_decodereturns apem/rsafile as a tuple of lines? If so, I think I’m getting it. I’m primarily an application developer and I just couldn’t understand why you would ever not know the length of the tuple you were working with and why you would ever have a one longer than 3 or 4. I definitely need to work on my phrasing when asking questions.D4no0
A real-life example that I encountered not so long ago was related to parse things from OTP’s
.
:public_keymodule: x509/lib/x509/public_key.ex at v0.8.9 · voltone/x509 · GitHub , even though in lots of cases you can get away with having records, sometimes you want a fast and dirty solution by usingelem/2I also think that we in elixir always tend to use alternative like lists instead of tuples for structured and ordered data, and honestly a lot of designs could benefit a lot by using tuples.
I also had no idea that records were so useful, I encountered them a few times in the wild while trying to port erlang records to elixir and I always thought that this is just a interoperability bridge but it seems they can be very useful, I will most probably start incorporating them in my code from now on.
sodapopcan
Thanks Zach! A real world example-usage of large tuples of unknown size is exactly what I was looking for, I just didn’t ask the question very well …and I may or may not have been a little tipsy when I opened the topic
I couldn’t for the life of me think of an example for myself. I guess 
elem/2can stayAnd hey, I use that project you contribute to!
dimitarvp
Aha, I see, reading files (and the like) into memory.
I was thinking about how e.g. Floki does stuff where you always get a recursive tri-tuple.
zachallaun
See here; the tuple size is however many lines the document is long. So a file with 100 lines will have a 100-element tuple. A file with 2000 lines will have a 2000-element tuple.
dimitarvp
I still don’t get this, how could you not know? I mean there are several variants, right? It’s not like these variants are 1000+. What’s the problem with multi-headed
case?