Disclaimer: I’m going through some old posts that were in draft and publishing one’s which look relatively complete in case they’re of use: This post may not be 100% complete but does give a good overview of Elixir collections.
Lists in Elixir are implemented as linked lists which handle handle different types.
[3, "Three" :three]
Prepending to a list is faster than appending
list = [3, "Three" :three] ["pre" | list]
Appending
list = [3, "Three" :three] list ++ ["post"]
List concat
[3, "Three" :three] ++ ["four", :4, 4]
List subtraction,
[2] -- [2.0]
Head and tail
hd [3, "Three" :three] tl [3, "Three" :three]
Pattern matching
We can split the head an tail using Z
[head | tail] = [3.14, :pie, "Apple"]
The equivalent of a dictionary known as keyword lists in Elixir
[foo: "bar", hello: "world"] [{:foo, "bar"}, {:hello, "world"}]
Keys can be atoms, keys are ordered and do not have to be unique
Maps
Unlike keyword lists they allows keys of any type and are unordered the syntax for a ,ap is %{}
map = %{:foo => "bar", "hello" => :world}