anurag.peshne

anurag.peshne

Arrays

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.

Marked As Solved

rvirding

rvirding

Creator of Erlang

For what you describe a tuple actually seems like a good choice. Some questions though:

  1. How many elements in this “array”?
  2. How static is this “array” to be?
  3. Is it to be globally accessed or through one process?
  4. When randomly picking a process is it enough just to generate a random number and use that as index?
  5. Is there any other semantics to this “array” than just storing the pids and picking one randomly?

Having very large tuples is inefficient when you update them. You basically make a copy of the whole tuple every time, but not the elements themselves of course. If it is relatively static then a tuple will work fine, doing your occasional deletes would probably work fine. Otherwise the :array module mentioned by @NobbZ is a good alternative. Maybe just using a map would work as well.

Tuples :array’s and maps are local to one process so if multiple processes need to access it then perhaps using an ETS table would be better. These can be globally accessed however they have no built in transaction mechanism so you might need a process to manage them.

Also Liked

NobbZ

NobbZ

As a direct answer to your question: there is the :array module from erlang.

That module uses a set of nested tuples and reading is O(1). I’m not sure about its writing runtime though, and you’d have to implement filling logic on delete by yourself (either take the last in the hole, or let every following item go one step forward, thats up to you, depending on if original order matters).

There is also the array package which is a wrapper around the erlang module, providing some protocols as well to make it easier to handle them from elixir site. But it hasn’t been updated in the last 3 years, therefore it might spit some warnings in a current elixir or not work at all.


To try to find another solution to your problem. It sounds a bit as if you want to create some kind of load balancer which is giving away its work randomly on the workers.

By picking randomly from a fixed set, you might get non-uniform distribution due to how PRNGs work.

Therefore I’d suggest one of these solutions:

  1. Only shuffle once, use that as a base for roundrobbin. You can use :queue module for a datastructure which can pull from the head and push to the tail in amortized o(1).
  2. Shuffle the original list and keep a copy of it around. Each time you need to send a job, use the head of the shuffled list until it is empty, then reshuffle the original list and keep going.
Qqwy

Qqwy

TypeCheck Core Team

I have two fine packages for you:

  • Arrays exposes a common array interface, with multiple different implementations that you can try if the default performance characteristics are not to your liking. (The two built-in implementations are a map-based implementation and an :arrays-based implementation.)

This is probably perfect for your needs, although the last time I had time to work on it I was unable to write proper documentation and tests, so these are still lacking.

If you need to do more mathematical stuff with them:

  • Tensor gives you one-dimensional vectors, two-dimensional matrices and n-dimensional tensors, and defines many common operations on them. Tensor stores these in a sparse format, which may or may not be suited well to your problem domain.

Tensor is very stable and greatly documented and tested.

kylethebaker

kylethebaker

Does order matter? Do you need to access items by key or just at random? Map has been mentioned, but there is also MapSet if you don’t need keys. Both implement the enumerable protocol, so to get a random value you can pass it to Enum.random. What I’ve done in the past when I want constant time lookups by key and also maintain order is to create two structures: a map for the constant lookups, and a list of keys for the order.

You may also consider looking into using a Registry if you want a container for processes, though for your use case it might be easier to just keep simple collection of pids.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement