what is the closest thing to Arrays in Elixir. By arrays I mean, a container for values which I can access in constant time.
I've looked at tuple, but according to the documentation:
Tuples are not meant to be used as a “collection” type (which is also suggested by the absence of an implementation of the Enumerable protocol for tuples): they’re mostly meant to be used as a fixed-size container for multiple elements.
What I actually want to do: I want to store n processes in an array and periodically pick a random process and send it a message. I'm open to other suggestions too.
Arrays is a library to work with well-structured Arrays with fast random-element-access for Elixir, offering a common interface with multiple implementations with varying performance guarantees that can be switched in your configuration.
The length() function returns the length of the list that is passed as a parameter.
There is no way of appending an element to a list in Erlang either. In Elixir and Erlang we use `list ++ [elem]` to append elements.
I ended up using a combination of list
and registry
since I was working with processes. I got many responses on Elixir forum which I'm listing below for future reference:
Tuple
: stored continuous in memory, constant access time, editing results in copying whole structure. Does not implement Enumerable protocol.List
: O(n) access time, prefixing is cheaper than suffixing. Implements Enumerable protocol.Map
: O(log n) read, write, delete time. Also implements Enumerable protocol.:array
: array
module from Erlang.registry
: (applicable only if storing processes) A local, decentralized and scalable key-value process storage.Also, note 2 and 3 (List and Map) are persistent data structures.
There are also two Elixir packages Arrays and Tensor that provide similar functionalities.
Elixir has an array module via erlang: http://erlang.org/doc/man/array.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With