Re: [elixir-core:11535] Support using brackets to access an index of a list

2023-09-21 Thread 'Justin Wood' via elixir-lang-core
> Languages that support it via square brackets: Rust, Ruby, Javascript, > Python, C, Julia. All of these languages (other than maybe Julia? I have not used it at all.) are actually using arrays and not lists. It is fairly natural to have easy index based lookup for arrays. After all, it is

Re: [elixir-core:11537] Support using brackets to access an index of a list

2023-09-21 Thread Marten Wijnja
On 21/09/2023 20:19, 'Justin Wood' via elixir-lang-core wrote: Depending on what exactly you are doing, and the size of your lists, it may be worth taking a look at the :array module provided by Erlang. It will give you logarithmic access times, or O(log n). But it still has the downside of

Re: [elixir-core:11539] Support using brackets to access an index of a list

2023-09-21 Thread Zach Daniel
Nice . That is a definite improvement. Ultimately, I think it would be a nice improvement to the language, but I don't disagree with the idea that we need to take great care for the on ramp and to help people avoid footguns. I don't think its very likely that I'll convince the team to add it,

Re: [elixir-core:11538] Support using brackets to access an index of a list

2023-09-21 Thread José Valim
For what is worth, the error message on main says: --- iex(1)> [1, 2, 3][0] ** (ArgumentError) the Access module does not support accessing lists by index, got: 0 Accessing a list by index is typically discouraged in Elixir, instead we prefer to use the Enum module to manipulate lists as a

Re: [elixir-core:11534] Support using brackets to access an index of a list

2023-09-21 Thread Zach Daniel
Something else I would say: if we are doing this to protect people from writing non-idiomatic code, can we improve the error message for when you provide an integer to bracket access a list? Something like: If you are trying to get an element from a list by index, use ` Enum.at (

Re: [elixir-core:11536] Support using brackets to access an index of a list

2023-09-21 Thread Zach Daniel
Yeah, I'm familiar with the differences on that front, but at the end of the day there are plenty of times where its perfectly normal to get a list/tuple element by index. It seems like a natural syntax extension to me (the syntax is basically already there, just intentionally blocked off in a

Re: [elixir-core:11533] Support using brackets to access an index of a list

2023-09-21 Thread Zach Daniel
I mean ` Enum.at/2` ( http://enum.at/2` )  On Thu, Sep 21, 2023 at 1:14 PM, Zach Daniel < zachary.s.dan...@gmail.com > wrote: > > Languages that support it via square brackets: Rust, Ruby, Javascript, > Python, C, Julia. > > > I see your point, ultimately for me what is frustrating is when

[elixir-core:11530] Support using brackets to access an index of a list

2023-09-21 Thread Zach Daniel
Right now if you use access on a list, it expects it to be a keyword and requires the value to be an atom. I've tried to think of a reason why we couldn't support this in the language, but I haven't been able to. If the value being accessed is an `integer` it could get the item at that index,

Re: [elixir-core:11532] Support using brackets to access an index of a list

2023-09-21 Thread Zach Daniel
Languages that support it via square brackets: Rust, Ruby, Javascript, Python, C, Julia. I see your point, ultimately for me what is frustrating is when I have predefined data structures and we have a tool for "get the value at some point (i.e map keys)" so I can do `map[:key][:key]` but if I

Re: [elixir-core:11530] Support using brackets to access an index of a list

2023-09-21 Thread José Valim
Index-based access on a list is very frequently an anti-pattern. Allowing list[i] means it will be easier to write non-efficient versions of algorithms without a second thought for those coming from imperative languages, instead of using the functions in Enum. It means someone can write this: for

Re: [elixir-core:11531] Support using brackets to access an index of a list

2023-09-21 Thread 'Justin Wood' via elixir-lang-core
> It was pointed out that perhaps we don't do this to express that indexing a > list is not fast in Elixir like it is in other languages, but I'm not sure if > that is sufficient reason IMO to leave out a typically very standard feature > of lists. > > Thoughts? Can you give an example of a