Powered by AppSignal & Oban Pro

W1D3 Review

w1d3review.livemd

W1D3 Review

Lists

warning: the underscored variable “_tail” is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore #cell:3

[a, b | _tail] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2] -- [2]
[1] -- [1]
[1] -- [2]
[1] -- [2]
[1, 2] -- [1]
[1, 2] -- [1] -- [2]
[1, 2] -- [1]
[2]
[1, 2] -- ([1] ++ [2])
[1, 2] -- [1, 2]
[]
([2, 1] -- [1]) -- [2]
([1, 2] -- [1]) -- [2]
[1, 2] -- [2, 1]

Keyword Lists vs Maps

[] ++ [bananas: 2] ++ [bananas: 3]
[one: 1, one: 2]
%{one: 1, one: 2}

Shopping List

shopping_cart = []

shopping_cart = shopping_cart ++ ["grapes", "walnuts", "apples"]
shopping_cart = shopping_cart ++ ["blueberries", "chocolate", "pizza"]
shopping_cart = shopping_cart -- ["grapes", "walnuts"]
shopping_cart = shopping_cart ++ ["banana", "banana", "banana"]

[]
|> Kernel.++(["grapes", "walnuts", "apples"])
|> Kernel.++(["blueberries", "chocolate", "pizza"])
|> Kernel.--(["grapes", "walnuts"])
|> Kernel.++(["banana", "banana", "banana"])

Family Tree

  • formatter and one lining

Mazes

map = %{"one" => %{"two" => "Exit!"}}
map["one"]["two"]

map = %{one: %{two: "Exit"}}
map.one.two
[{:key, "value"}]
[key: "value"]
%{key: "value"}
%{"key" => "value"}
%{1 => 3}

Why can we use key: "value" syntax with a map.

%{:key => "value"} == %{key: "value"}

can I access a map’s key using a variable? (yes)

map = %{key1: "value"}

key = :key1

map[key]
map = %{key1: "value"}
new_map = %{map | key1: "updated value"}

# map
new_map

Treasure Map

map = %{{"turn", "right"} => %{dig: "gold"}}

# map[{"trun", "right"}].dig

nil.dig()