4 ms·
Elixir also doesn't have arrays so you have to fall back on Erlang's extremely clunky variant or, worse, shoehorn a map.
by cutler 2y ago
Elixir also doesn't have arrays so you have to fall back on Erlang's extremely clunky variant or, worse, shoehorn a map.
- peoplefromibiza 2y agomany 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
- derekkraan 2y agoYeah, 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.
- gregors 2y agoI'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.
- foldr 2y agoNot 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.
- rdtsc 2y ago> 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.
- Squeeeez 2y agoAre you sure you mean O(n) instead of O(1), which I would expect for an array?
- foldr 2y agoOops! Yes, O(1). Too late to edit, unfortunately.
- mikhailfranco 2y agoTuples can be up to 16,777,215 elements, so quite substantial applications. https://www.erlang.org/doc/system/system_limits 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 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.
- out_of_protocol 2y agoThere are arrays - https://www.erlang.org/doc/apps/stdlib/array.html https://www.erlang.org/doc/apps/stdlib/array.html Just not used across the language
- mikhailfranco 2y agoThey 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).
- pjmlp 2y agoThat 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.
- gregors 2y agoBesides arrays existing in Erlang, there's a nice elixir library if you need it: https://www.erlang.org/doc/apps/stdlib/array.html https://www.erlang.org/doc/apps/stdlib/array.html https://hexdocs.pm/arrays/Arrays.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/26195 https://elixirforum.com/t/elixir-array-like-data-structure/2...
- gregors 2y ago>>> 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-arrays-primitives-strings-objects https://stackoverflow.com/questions/5048371/are-javascript-a...