[Proposal] Add Tuple.replace_at

35 views
Skip to first unread message

Rudolf Manusadzhian

unread,
Jun 10, 2022, 3:25:11 PM6/10/22
to elixir-lang-core
Hi there. Recently I faced the situation where I wanted to "replace" the element of a tuple that was created dynamically from a list with the length defined at runtime.

While I found :erlang.setelement(index, tuple, value) does what I need, its arguments order and 1-based indexes are not that natural to elixir.

So I would propose to add a wrapper around :erlang.setelement/3 to Tuple module. (I called it replace_at just because I was looking for the analogy of List.replace_at but for tuple)

I put together quickly a commit of how it could be implemented https://github.com/elixir-lang/elixir/commit/6e498edaf257e3fccf15bc5243c3ccf2d67e6e85

Andrey Yugai

unread,
Jun 10, 2022, 3:40:11 PM6/10/22
to elixir-l...@googlegroups.com
Hi Rudolf. Elixir actually has such wrapper already, and what's more cool about it, it's implemented exactly how you suggested:

https://github.com/elixir-lang/elixir/blob/a64d42f5d3cb6c32752af9d3312897e8cd5bb7ec/lib/elixir/lib/kernel.ex#L1724


Other tuple related functions are mentioned at the top of Tuple module docs.



-------- Original Message --------
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/58227783-b0bf-4d0f-b1cb-df15f4df0f97n%40googlegroups.com.

Rudolf Manusadzhian

unread,
Jun 10, 2022, 7:32:49 PM6/10/22
to elixir-lang-core
Wow, awesome! Thank you, Andrey! 
Reply all
Reply to author
Forward
0 new messages