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 25 to 16

slouchpie

slouchpie

That is weird. I have the same behavior. I can not let this go. Why does :pub not “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_key is in its own “public_key” application.

Similarly, :pretty_pr, :os_sup and :release_handler will not tab out.

sodapopcan

sodapopcan OP

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 stdlib :upside_down_face:

D4no0

D4no0

:thinking: I don’t know what you consider standard library, for me everything included in elixir and OTP is standard library, :public_key powers 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

cmo

One of the caching libs, cachex or nebulex, uses records. For performance reasons I presume.

sodapopcan

sodapopcan OP

Thanks for more examples! :public_key doesn’t appear to be in the standard lib but I take it :public_key.pem_decode returns a pem/rsa file 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

D4no0

A real-life example that I encountered not so long ago was related to parse things from OTP’s :public_key module: 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 using elem/2 :grin: .

I 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

sodapopcan OP

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 :grimacing: I couldn’t for the life of me think of an example for myself. I guess elem/2 can stay :wink:

And hey, I use that project you contribute to!

dimitarvp

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

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

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?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews