Elixir Data Collection

In Elixir, there are multiple basic data structures to use for storing data collection.

  • List
  • Tuples
  • Dict
    • Keyword List
    • Map

As a newbie, I am not yet feel familiar with them yet as they are use differently compared to when they are used in my previous langauges. Thus, writting a short brief about them is good for me.

List

List in Elixir is a linked list, a very common data structure for every computer science students. It is commonly used in Elixir, as how the array is used in other language. Defining a list can be done by:

1
list = [10, 20, 30, "a", [1, 2, 3]]

As you may know, (linked)list allows only a sequential access to its children. Random access like how you do with array is not available.

Instead, list traversal is done using a head-tail mapping.

1
2
3
4
5
6
7
8
9
10
list = [1, 2, [5, 6]]

[head|tail] = list
# head = 1, tail = [2, [5, 6]]

[head|tail] = tail
# head = 2, tail = [5,6]

[head|tail] = tail
# head = [5, 6], tail = []

Two more basic operations are list concatination and removal.

1
2
3
4
5
6
7
8
9
10
list = [1, 2, 3, 4]

list2 = [10, 11] ++ list
# Faster: list2 = [10, 11, 1, 2, 3, 4]

list3 = list ++ [5, 6, 1]
# Slower: list3  = [1, 2, 3, 4, 5, 6, 1]

list4 = list -- [2, 3]]
# list4 = [1, 4]

The interesting part on these operations are the sequence when the operation is invoked. Concatination is very fast if it happen at the head of the list. On the other hands, it will be slower if it happen at the end of the list, as we need to traverse to there before operating. Adding the item between the list is expensive too. For removal, things happen from the head of the list.

1
2
3
4
5
6
7
8
9
10
list = [1, 2, 3, 4, 1]

list2 = list - [1]
# list2 = [2, 3, 4, 1]

list3 = list - [1, 1]
# list3 = [2, 3, 4]

list4 = list - [1, 1, 1]
# list4 = [2, 3, 4]

Getting the size of the list is also considered expensive as the list is needed to be traversed from the beginning to the end to get the total number of item.

Tuples

For one who studied mathematics in the university, tuples should feel very familair. Tuples is also used to hold a collection. Unlikely to list, tuples uses a continuous memory space to store the data. Thus, random access and calculating length is faster.

1
2
3
tup = {1, 2, 3, 4}
second_item = elem(tup, 1)
# second_item = 2

Keyword List

Keyword list is actually a list of 2-tuples.

Comments