Mix.install([
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:tested_cell, github: "brooklinjazz/tested_cell"},
{:utils, path: "#{__DIR__}/../utils"}
])
Ensure you type the ea
keyboard shortcut to evaluate all Elixir cells before starting. Alternatively, you can evaluate the Elixir cells as you read.
Keyword lists are lists with keys. Each key associates with a value.
flowchart
key1: --> Value1
key2: --> Value2
key3: --> Value3
Keyword lists belong to a family of data types known as associative data structures. you'll also often hear it referred to as a key-value data structure.
In Elixir, the key is an atom, and the value can be any data structure, even another keyword list.
[key: "value"]
[atom: "anything!"]
["You can use quotes for the atom key!": ""]
[snake_case_is_convention: ""]
[ButThatIsNotEnforced: ""]
Keys in a keyword list do not have to be unique.
[my_key: "value", my_key: "value"]
Elixir allows you to work with keyword lists in this nice [key: "value"]
syntax. However,
keyword lists are actually just a list of tuples where the first element is an atom.
[{:key, "value"}]
In the Elixir cell below, you can see how Elixir converts lists of tuples matching the {:atom, value}
syntax
into a keyword list.
[{:key, "value"}, {:key, "value"}]
If you break the convention in the keyword list, Elixir reverts to displaying the keyword list as a list of tuples.
[{}, key: "value"]
The keyword list syntax must come at the end of a list, or we'll cause a SyntaxError.
[key: "value", ""]
List order is guaranteed, so the same is true for keyword lists.
In the Elixir cell below, create a keyword list of your favourite super hero. Include their :name
and :secret_identity
.
Example solution
[name: "Spiderman", secret_identity: "Peter Parker"]
Enter your solution below.
We can also use --
and ++
list operators with keyword lists.
[one: 1, two: 2] -- [one: 1]
If you add a list to a keyword list, you'll notice that the keyword list element will evaluate as a tuple again. Remember that keyword lists are simply lists of tuples with an atom and a value.
[one: 1] ++ [1]
In the Elixir cell below, use ++
to add [one: 1]
to [two: 2]
to make [one: 1, two: 2]
.
Remove [two: 2]
from [one: 1, two: 2]
to make [one: 1]
.
Much like with lists, we can use pattern matching to bind values in a keyword list to variables.
[hello: my_variable] = [hello: "world"]
my_variable
Much like a list, we must match on the entire keyword list, otherwise we'll cause a MatchError.
[one: one] = [one: 1, two: 2]
So long as we have a match for every element in the keyword list, we can use pattern matching.
[one: one, two: two] = [one: 1, two: 2]
We can use the [head | tail]
syntax to pattern match on the tail of the keyword list to avoid this issue.
[head | _tail] = [one: 1, two: 2]
head
If we want to pattern match on the keys in the list, we need to use the tuple syntax on the left hand side of the =
match operator.
[{key, value}] = [key: "value"]
key
value
Bind 1
in the following keyword list to a variable one
.
Example solution
[{_, one} | _tail] = [one: 1, two: 2, three: 3, four: 4]
Enter your solution below.
[one: 1, two: 2, three: 3, four: 4]
Consider the following resource(s) to deepen your understanding of the topic.
Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish keyword lists section"
Previous | Next |
---|---|
Lists | Maps |