More Complex List Patterns
Understand lists by implementing different kinds of operations in different cases.
We'll cover the following
The join operator |
Not every list problem can be easily solved by processing one element at a time. Fortunately, the join operator, |
, supports multiple values to its left. Thus, we can write the following:
iex> [ 1, 2, 3 | [ 4, 5, 6 ]]
[1, 2, 3, 4, 5, 6]
The same thing works in patterns, so we can match multiple individual elements as the head
. For example, the following program swaps pairs of values in a list.
Run the Swapper.swap([1,2])
command in the terminal below.
Get hands-on with 1400+ tech skills courses.