Move sigils to later in the lessons
While doing this, I also discovered that there is also a reference to lists in numbers, but that might a bit easier for someone to grasp, given the hint that the koan gives, and the output they see when they run it.
This commit is contained in:
47
lib/koans/07_keyword_lists.ex
Normal file
47
lib/koans/07_keyword_lists.ex
Normal file
@@ -0,0 +1,47 @@
|
||||
defmodule KeywordLists do
|
||||
use Koans
|
||||
|
||||
@intro "KeywordLists"
|
||||
|
||||
koan "Like maps, keyword lists are key-value pairs" do
|
||||
kw_list = [foo: "bar"]
|
||||
|
||||
assert kw_list[:foo] == ___
|
||||
end
|
||||
|
||||
koan "Keys may be repeated, but only the first is accessed" do
|
||||
kw_list = [foo: "bar", foo: "baz"]
|
||||
|
||||
assert kw_list[:foo] == ___
|
||||
end
|
||||
|
||||
koan "You could access a second key by removing the first" do
|
||||
kw_list = [foo: "bar", foo: "baz"]
|
||||
[_|kw_list] = kw_list
|
||||
|
||||
assert kw_list[:foo] == ___
|
||||
end
|
||||
|
||||
koan "Keyword lists are just special syntax for lists of two-element tuples" do
|
||||
assert [foo: "bar"] == [{___, ___}]
|
||||
end
|
||||
|
||||
koan "But unlike maps, the keys in keyword lists must be atoms" do
|
||||
not_kw_list = [{"foo", "bar"}]
|
||||
|
||||
assert_raise ArgumentError, fn -> not_kw_list[___] end
|
||||
end
|
||||
|
||||
koan "Conveniently keyword lists can be used for function options" do
|
||||
transform = fn str, opts ->
|
||||
if opts[:upcase] do
|
||||
String.upcase(str)
|
||||
else
|
||||
str
|
||||
end
|
||||
end
|
||||
|
||||
assert transform.("good", upcase: true) == ___
|
||||
assert transform.("good", upcase: false) == ___
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user