Missing tuple access idioms

33 views
Skip to first unread message

Mário Guimarães

unread,
Apr 5, 2019, 6:29:56 AM4/5/19
to elixir-lang-core
Hello,

any reason why there are no easy readable idioms like

first, snd, third, ..., tenth (I guess is enough)

to access tuples?

Thanks

Louis Pilfold

unread,
Apr 5, 2019, 7:05:35 AM4/5/19
to elixir-lang-core
Hey

We have elem/2, which is quite clear and more concise.

We don't need specific functions for each index because we are not limited by a static type system and so the index does not need to be known at compile time.

Cheers,
Louis

--
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/75b06fc7-5be0-4070-8c49-6e2fb5779ea3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arjan Scherpenisse

unread,
Apr 5, 2019, 7:13:49 AM4/5/19
to elixir-lang-core
If you really want this, you could metaprogram it yourself ;-)

defmodule TupleAccess do
  @names ~w(first second third fourth fifth sixth seventh eight ninth tenth)a

  for {name, index} <- Enum.with_index(@names) do
    def unquote(name)(tuple) do
      elem(tuple, unquote(index))
    end
  end
end

iex(5)> import TupleAccess
TupleAccess
iex(6)> first({:a})
:a
iex(7)> first({:a, :b})
:a
iex(8)> second({:a, :b})
:b


On Friday, April 5, 2019 at 1:05:35 PM UTC+2, Louis Pilfold wrote:
Hey

We have elem/2, which is quite clear and more concise.

We don't need specific functions for each index because we are not limited by a static type system and so the index does not need to be known at compile time.

Cheers,
Louis

On Fri, 5 Apr 2019, 11:29 Mário Guimarães, <mario.lui...@gmail.com> wrote:
Hello,

any reason why there are no easy readable idioms like

first, snd, third, ..., tenth (I guess is enough)

to access tuples?

Thanks

--
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-l...@googlegroups.com.

José Valim

unread,
Apr 5, 2019, 7:42:11 AM4/5/19
to elixir-l...@googlegroups.com
But perhaps, the most important, is that generally you should be pattern matching on tuples.

Most of the times, you know exactly the size of the tuple you are working with, and pattern matching on it is more intention revealing.

If you have large tuples and the size may change, then sure, pattern matching is not a good idea, but then the Record module becomes handy. Record allows you to work with tuples and give also them names, and the names are a better API than relying on indexes.

José Valim
Skype: jv.ptec
Founder and Director of R&D


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/da9cc177-513d-4683-8f4e-1c8b9c8286ca%40googlegroups.com.

Mário Guimarães

unread,
Apr 5, 2019, 8:46:56 AM4/5/19
to elixir-lang-core
Hi José

there are occasions like when doing ... |> Enum.map(&first) |> ... where pattern matching does not apply, and .. |> Enum.map(&elem(&1,0)) |> ... is much less readable.

It was regarding these cases that I thought about tuple accessors.

Mário Guimarães

Andrea Leopardi

unread,
Apr 5, 2019, 8:48:32 AM4/5/19
to elixir-l...@googlegroups.com
Often times matching on the tuple size can make your program safer since it will crash on unexpected inputs. In cases like the one you mentioned, I would go with |> Enum.map(fn {x, _, _} -> x end).

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/876d9efd-f721-431a-b4c5-620876f2aaad%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Andrea Leopardi

Mário Guimarães

unread,
Apr 5, 2019, 9:36:21 AM4/5/19
to elixir-lang-core
I guess there will be always different tastes regarding this subject :-)


sexta-feira, 5 de Abril de 2019 às 13:48:32 UTC+1, Andrea Leopardi escreveu:
Often times matching on the tuple size can make your program safer since it will crash on unexpected inputs. In cases like the one you mentioned, I would go with |> Enum.map(fn {x, _, _} -> x end).
--

Andrea Leopardi
Reply all
Reply to author
Forward
0 new messages