Merge pull request #199 from codefupanda/master

Fetching values of repeated key in keywordlist
This commit is contained in:
Felipe Seré
2017-07-15 11:22:59 +01:00
committed by GitHub
3 changed files with 11 additions and 4 deletions

View File

@@ -15,11 +15,11 @@ defmodule KeywordLists do
assert kw_list[:foo] == ___ assert kw_list[:foo] == ___
end end
koan "You could access a second key by removing the first" do koan "You could access the values of repeating key" do
kw_list = [foo: "bar", foo: "baz"] kw_list = [foo: "bar", foo1: "bar1", foo: "baz"]
[_|kw_list] = kw_list values = Keyword.get_values(kw_list, :foo)
assert kw_list[:foo] == ___ assert List.last(values) == ___
end end
koan "Keyword lists are just special syntax for lists of two-element tuples" do koan "Keyword lists are just special syntax for lists of two-element tuples" do

View File

@@ -45,6 +45,12 @@ defmodule PatternMatching do
end end
end end
koan "So does the keyword lists" do
kw_list = [type: "car", year: 2016, make: "Honda"]
[_type | [_year | [tuple]]] = kw_list
assert tuple == {___, ___}
end
koan "The pattern can make assertions about what it expects" do koan "The pattern can make assertions about what it expects" do
assert match?([1, _second, _third], ___) assert match?([1, _second, _third], ___)
end end

View File

@@ -11,6 +11,7 @@ defmodule PatternsTests do
"eggs, milk", "eggs, milk",
"Honda", "Honda",
MatchError, MatchError,
{:multiple, [:make, "Honda"]},
[1,2,3], [1,2,3],
{:multiple, ["Meow", "Woof", "Eh?"]}, {:multiple, ["Meow", "Woof", "Eh?"]},
{:multiple, ["Mickey", "Donald", "I need a name!"]}, {:multiple, ["Mickey", "Donald", "I need a name!"]},