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 way) for when you 
have to do that. In the same way that keyword lists aren't typically long, 
plenty of lists are not too long for it to be perfectly fine to access an item 
by index.

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

> 
> 
> 
> 
>> 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 meant to be a
> contiguous block of memory with each element having a known size. At least
> for Rust and C, other languages may be more loose in terms of
> implementation.
> 
> 
> 
> In fact, when you use std::collections::LinkedList in rust, you do not
> have this kind of syntax. If you want to find the element at index i you
> would need to iterate over the list just like ocaml. There is no provided
> function to my knowledge to even access a random element.
> 
> 
> 
> When you choose a random element from an array, you just need to do some
> math in order to find the offset in memory for where the element you are
> looking for resides. This means that random access is constant time or
> O(1) if you prefer. Having a convenient syntax for this makes sense since
> the time complexity is so low.
> 
> 
> 
> Linked lists, which Elixir and Erlang use, are not necessarily stored as a
> single contiguous block of memory. It is essentially a value and a pointer
> to the next element in the list. This means you actually need to traverse
> the list to the point that you want to access. This means that random
> access is considered to be linear time or O(n). Having a convenient syntax
> for this makes less sense since the time complexity is significantly
> higher. This is the tradeoff that most functional languages have taken,
> make it harder to do the expensive thing and get people to think about the
> data structures being used.
> 
> 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 not having [] type access.
> 
> 
> 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/ 311c75ed-b0fc-4660-945d-b190e294b939%40app. fastmail.
> com (
> https://groups.google.com/d/msgid/elixir-lang-core/311c75ed-b0fc-4660-945d-b190e294b939%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/lmti0ktg.6abcce21-744d-4e3f-8e5e-ddb2ed935cc9%40we.are.superhuman.com.

Reply via email to