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 want to write 
`map[:key][0][:key][0]` I have to reorient the code entirely to use `get_in` or 
piped functions like `|> Map.get(:key)`. Getting a key out of a keyword list 
would be equally as slow as getting a list at an index, so it didn't seem to me 
like the purpose of the `[]` was to only do fast operations.

There are plenty of cases where I need to get things positionally out of lists, 
the two that come to mind are handling various data structures that I got 
externally, like from an API (I can use bracket access until I get to a list 
then I can't) and testing. Often I'm testing that something at a very specific 
point in a produced data structure is a specific value. I.e "the tenth item at 
the path `foo[:bar]` is X". I can do that with pattern matching, but its only 
feasible to a point (like pattern matching the 10th item from a list does not 
look good). We can use `get_in` but we could also make that argument for 
everything and not have bracket access at all. It seems like a natural 
extension of that choice to give lists (and probably tuples) sensible behavior 
with bracket syntax.

I'm not necessarily convinced that not having `list[i]` will ultimately 
convince people not to write those bad algorithms, because they can just as 
easily say ` List.at ( http://list.at/ ) (list, 1)`. But wether or not I'm 
convinced of that is not relevant 😆

On Thu, Sep 21, 2023 at 12:39 PM, Justin Wood < m...@ankhers.dev > wrote:

> 
> 
> 
> 
>> 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 language that supports this? In my
> experience, languages do not provide this.
> 
> 
> 
> In Haskell, you need to the non-standard !! operator.
> 
> 
> 
> myList !! 5
> 
> 
> 
> OCaml does not provide a way to do this at all and you need to manually
> iterate through the list to find the element you want (maybe other stdlibs
> like Core provide something, but I have not used them)
> 
> 
> 
> Racket has the list-ref function in order to accomplish this
> 
> 
> 
> (list-ref my-list 3)
> 
> 
> 
> Common Lisp has the nth function for this
> 
> 
> 
> (nth 5 my-list)
> 
> 
> 
> In comparison, Elixir has provided the Enum. at/ 2 ( http://enum.at/2 ) 
> function.
> 
> 
> 
> 
> While I believe most languages do provide some facility to retrieve the
> value at some index of a list, most do not provide it as square bracket
> indexing you would see in other languages where arrays are more common
> such as Ruby or Javascript.
> 
> 
> 
> Justin
> 
> 
> 
> 
> 
> --
> 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+unsubscribe@ googlegroups. com (
> elixir-lang-core+unsubscr...@googlegroups.com ).
> To view this discussion on the web visit https:/ / groups. google. com/ d/
> msgid/ elixir-lang-core/ dc142043-83dc-48e6-8c3f-7a39c8376d0e%40app. fastmail.
> com (
> https://groups.google.com/d/msgid/elixir-lang-core/dc142043-83dc-48e6-8c3f-7a39c8376d0e%40app.fastmail.com?utm_medium=email&utm_source=footer
> ).
>

-- 
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/lmtev59d.9a7191c8-acee-4bb9-9673-4c6db8277641%40we.are.superhuman.com.

Reply via email to