Tutorial by Examples

Keyword lists are lists where each item in the list is a tuple of an atom followed by a value. keyword_list = [{:a, 123}, {:b, 456}, {:c, 789}] A shorthand notation for writing keyword lists is as follows: keyword_list = [a: 123, b: 456, c: 789] Keyword lists are useful for creating ordered ...
Strings in Elixir are "binaries". However, in Erlang code, strings are traditionally "char lists", so when calling Erlang functions, you may have to use char lists instead of regular Elixir strings. While regular strings are written using double quotes ", char lists are wr...
Lists in Elixir are linked lists. This means that each item in a list consists of a value, followed by a pointer to the next item in the list. This is implemented in Elixir using cons cells. Cons cells are simple data structures with a "left" and a "right" value, or a "he...
map is a function in functional programming which given a list and a function, returns a new list with the function applied to each item in that list. In Elixir, the map/2 function is in the Enum module. iex> Enum.map([1, 2, 3, 4], fn(x) -> x + 1 end) [2, 3, 4, 5] Using the alternative c...
Elixir doesn't have loops. Instead of them for lists there are great Enum and List modules, but there are also List Comprehensions. List Comprehensions can be useful to: create new lists iex(1)> for value <- [1, 2, 3], do: value + 1 [2, 3, 4] filtering lists, using guard expressi...
iex> [1, 2, 3] -- [1, 3] [2] -- removes the first occurrence of an item on the left list for each item on the right.
Use in operator to check if an element is a member of a list. iex> 2 in [1, 2, 3] true iex> "bob" in [1, 2, 3] false
Use Enum.chunk/2 to group elements into sub-lists, and Map.new/2 to convert it into a Map: [1, 2, 3, 4, 5, 6] |> Enum.chunk(2) |> Map.new(fn [k, v] -> {k, v} end) Would give: %{1 => 2, 3 => 4, 5 => 6}

Page 1 of 1