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 sexta-feira, 5 de Abril de 2019 às 12:42:11 UTC+1, José Valim escreveu: > > 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* > www.plataformatec.com.br > Skype: jv.ptec > Founder and Director of R&D > > > On Fri, Apr 5, 2019 at 1:13 PM Arjan Scherpenisse <scherp...@gmail.com > <javascript:>> wrote: > >> 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. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/elixir-lang-core/75b06fc7-5be0-4070-8c49-6e2fb5779ea3%40googlegroups.com >>>> >>>> <https://groups.google.com/d/msgid/elixir-lang-core/75b06fc7-5be0-4070-8c49-6e2fb5779ea3%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> -- >> 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 <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/da9cc177-513d-4683-8f4e-1c8b9c8286ca%40googlegroups.com >> >> <https://groups.google.com/d/msgid/elixir-lang-core/da9cc177-513d-4683-8f4e-1c8b9c8286ca%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- 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-core+unsubscr...@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.