News for hackers. Stuff that matters.
Re:Elixir Gotchas by cutler on Wednesday June 26, 2024 @06:42AM (#40798464)
Elixir also doesn't have arrays so you have to fall back on Erlang's extremely clunky variant or, worse, shoehorn a map.
Re:Elixir Gotchas by pjmlp on Wednesday June 26, 2024 @08:07AM (#40799020)
That is the curse of guest languages, either they have to deal with leaky abstractions, or have to create their own, slower, implementation on top of platform primitives.

Also why with time, I learned to stay with platform languages, even if they aren't as shinny, or have warts that in hindsight could have been done better.

Re:Elixir Gotchas by out_of_protocol on Wednesday June 26, 2024 @08:01AM (#40798965)
There are arrays - https://www.erlang.org/doc/apps/stdlib/array.html

Just not used across the language

Re:Elixir Gotchas by mikhailfranco on Wednesday June 26, 2024 @11:31AM (#40801193)
They are trees, so not O(1) access or update, more like O(logn), with some memory fragmentation for every update, insertion or deletion.

Tuples and binaries give O(1) read access, so depending on your use case, they may be better.

Also look at zippers, finger trees and ropes for more dynamic use, especially local operations (cursors).

Re:Elixir Gotchas by foldr on Wednesday June 26, 2024 @07:08AM (#40798616)
Not disagreeing with you, but it's worth noting that tuples support random O(n) access by index. They can therefore be used in place of arrays for some simple applications.
Re:Elixir Gotchas by rdtsc on Wednesday June 26, 2024 @08:20AM (#40799128)
> random O(n) access

O(1) for reading I believe. The slightly awkward array module is built on top of tuples, a 10-tree of tuples? or something like it.

But good point, once in a while I see list_to_tuple to provide faster indexed reads on lists.

Re:Elixir Gotchas by Squeeeez on Wednesday June 26, 2024 @09:04AM (#40799570)
Are you sure you mean O(n) instead of O(1), which I would expect for an array?
Re:Elixir Gotchas by foldr on Wednesday June 26, 2024 @09:13AM (#40799671)
Oops! Yes, O(1). Too late to edit, unfortunately.
Re:Elixir Gotchas by mikhailfranco on Wednesday June 26, 2024 @11:21AM (#40801060)
Tuples can be up to 16,777,215 elements, so quite substantial applications.

https://www.erlang.org/doc/system/system_limits

There is also some hope that future versions of the Erlang compiler will allow mutable O(1) update of tuple elements through setelement (code inside a process is - almost - guaranteed not to have shared references).

  Safe destructive update of tuples has been implemented
  in the compiler and runtime system. This allows the VM to
  update tuples in-place when it is safe to do so, thus 
  improving performance by doing less copying but also by 
  producing less garbage.
https://github.com/erlang/otp/releases?q=27&expanded=true

It should be possible to add Elixir syntactic sugar to make this more concise and natural.

Re:Elixir Gotchas by peoplefromibiza on Wednesday June 26, 2024 @06:57AM (#40798540)
many functional languages (if not all) do not have arrays (intended as a contiguous areas of memory containing indexable elements of fixed size) they are usually left as an implementation as libraries (i.e. Haskell's Data.Array)

There are also easy workarounds to that (including NIFs), I personally never needed arrays in Elixir

Re:Elixir Gotchas by derekkraan on Wednesday June 26, 2024 @10:15AM (#40800337)
Yeah, just (learn how to) use lists and you'll be fine.

Of course if your goal is to re-implement sorting algorithms that were designed for mutable memory, you will be disappointed.

This isn't a deal-breaker. If you want a platform language, use that. But for web, you'd be hard-pressed to find something better suited than Elixir.

Re:Elixir Gotchas by gregors on Wednesday June 26, 2024 @01:06PM (#40802106)
I've only ran into a single instance where I needed array-like access and since it was a read only type of thing tuples worked perfectly.
Re:Elixir Gotchas by gregors on Wednesday June 26, 2024 @01:02PM (#40802078)
Besides arrays existing in Erlang, there's a nice elixir library if you need it:

https://www.erlang.org/doc/apps/stdlib/array.html

https://hexdocs.pm/arrays/Arrays.html

Also, if it's read heavy, you can use use tuples for O(1) access.

Besides all that, the community is extremely helpful, if you have questions check out the forum, for example on this very topic there's quite a bit of helpful discussion

https://elixirforum.com/t/elixir-array-like-data-structure/2...

Re:Elixir Gotchas by gregors on Wednesday June 26, 2024 @01:12PM (#40802156)
>>> or, worse, shoehorn a map.

That's exactly what JavaScript does (as I understand it), but it hides it from you. And the implementation authors have optimized it over the years.

https://stackoverflow.com/questions/5048371/are-javascript-a...