Access behaviour for lists and tuples?

82 views
Skip to first unread message

Filip Haglund

unread,
Jun 26, 2016, 12:16:19 PM6/26/16
to elixir-lang-core
Why aren't lists and tuples implementing `Access`? 

{:a, :b, :c}[1] == :b


José Valim

unread,
Jun 26, 2016, 1:34:30 PM6/26/16
to elixir-l...@googlegroups.com
It is primarily for key-value based structures and not index based access.



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

On Sun, Jun 26, 2016 at 6:16 PM, Filip Haglund <fille....@gmail.com> wrote:
Why aren't lists and tuples implementing `Access`? 

{:a, :b, :c}[1] == :b


--
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/be88a1bc-68d3-4ff9-9a49-b704844a15fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Filip Haglund

unread,
Jun 27, 2016, 11:22:57 AM6/27/16
to elixir-lang-core
Would it hurt to add it?

Louis Pop

unread,
Jun 27, 2016, 11:27:32 AM6/27/16
to elixir-l...@googlegroups.com
Hey

I think adding the Access protocol to lists would imply that list
indexing is efficient. We should be careful to steer new users away
from techniques that are inefficient or not idiomatic.

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/3042a07d-90c9-4c82-a3a4-579579d44a1a%40googlegroups.com.

Filip Haglund

unread,
Jun 27, 2016, 8:06:15 PM6/27/16
to elixir-lang-core
Access for Keywords are already O(n), just like access on lists would be. Tuples would probably be O(1).

Sergii Boiko

unread,
Jun 28, 2016, 12:47:04 PM6/28/16
to elixir-lang-core
Access for tuples doesn't make sense, because in normal cases, you shouldn't use tuple as an iterative structure.
Good style is to have it <= 3 elements, and usually you need to switch to Struct in this case.
There is a use-case, when you want to have O(1) access and would use conversion from lists to tuples, but this is not a common use case, and still, can be quite easily achieved via Enums + access by index.
So, we should not support non-idiomatic usage of tuples.

Accessing lists by n is also non-idiomatic, because usually, you work with a list mostly in aggregate-style - iterating, mapping over it, or working in head/tail style via recursion.

Keywords lists are different, because they are mostly used on small amount of items and their use-case - find a value by key. Thus even having O(n) is ok for them.
Reply all
Reply to author
Forward
0 new messages