closest thing to arrays in Elixir

Viewed 6824

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.

4 Answers

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:

  1. Tuple: stored continuous in memory, constant access time, editing results in copying whole structure. Does not implement Enumerable protocol.
  2. linked-List: O(n) access time, prefixing is cheaper than suffixing. Implements Enumerable protocol.
  3. Map: O(log n) read, write, delete time. Also implements Enumerable protocol.
  4. :array: array module from Erlang.
  5. 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.

Like with mapping in the Solidity language, Elixir has map().

Related