-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps_lists_keywords.exs
35 lines (33 loc) · 1.09 KB
/
maps_lists_keywords.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
defmodule Canvas do
@defaults [fg: "black", bg: "white", font: "Times"]
def draw_text(text, options \\ []) do
options = Keyword.merge(@defaults, options)
IO.puts "Drawing text #{inspect(text)}"
IO.puts "Foreground: #{options[:fg]}"
IO.puts "Background: #{Keyword.get(options, :bg)}"
IO.puts "Font: #{Keyword.get(options, :font)}"
IO.puts "Pattern #{Keyword.get(options, :pattern, "solid")}"
IO.puts "Style #{inspect Keyword.get_values(options, :style)}"
end
end
defmodule InspectMaps do
@people [
%{ name: "Grumpy", height: 1.24 },
%{ name: "Dave", height: 1.88 },
%{ name: "Dopey", height: 1.32 },
%{ name: "Shaquille", height: 2.16 },
%{ name: "Sneezy", height: 1.28 }
]
def inspect_map() do
for people = %{height: height} <- @people, height > 1.5, do: people.name
end
end
defmodule PatternMatchingWithVariableKey do
def matching(foo) when foo == %{}, do: %{}
def matching(data = %{name: _name, state: _state, likes: _likes}) do
for key <- [ :name, :likes ] do
%{ ^key => value } = data
value
end
end
end