From 95e87c3fd8fcd16de28ba3776797c408c200c84a Mon Sep 17 00:00:00 2001 From: codefupanda Date: Tue, 11 Jul 2017 13:39:54 +0530 Subject: [PATCH 1/2] Fetching values of repeated key in keywordlist --- lib/koans/07_keyword_lists.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/koans/07_keyword_lists.ex b/lib/koans/07_keyword_lists.ex index 5cd2ec0..97680ae 100644 --- a/lib/koans/07_keyword_lists.ex +++ b/lib/koans/07_keyword_lists.ex @@ -15,11 +15,11 @@ defmodule KeywordLists do 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 + koan "You could access the values of repeating key" do + kw_list = [foo: "bar", foo1: "bar1", foo: "baz"] + values = Keyword.get_values(kw_list, :foo) - assert kw_list[:foo] == ___ + assert List.last(values) == ___ end koan "Keyword lists are just special syntax for lists of two-element tuples" do From 7a9f04dd45512caa450d2ad13d69dc9a3efcca17 Mon Sep 17 00:00:00 2001 From: codefupanda Date: Tue, 11 Jul 2017 22:05:07 +0530 Subject: [PATCH 2/2] Added pattern matching koan for keyword list --- lib/koans/12_pattern_matching.ex | 6 ++++++ test/koans/patterns_koans_test.exs | 1 + 2 files changed, 7 insertions(+) diff --git a/lib/koans/12_pattern_matching.ex b/lib/koans/12_pattern_matching.ex index 38f3a4a..74a7925 100644 --- a/lib/koans/12_pattern_matching.ex +++ b/lib/koans/12_pattern_matching.ex @@ -45,6 +45,12 @@ defmodule PatternMatching do 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 assert match?([1, _second, _third], ___) end diff --git a/test/koans/patterns_koans_test.exs b/test/koans/patterns_koans_test.exs index 789e458..c13c947 100644 --- a/test/koans/patterns_koans_test.exs +++ b/test/koans/patterns_koans_test.exs @@ -11,6 +11,7 @@ defmodule PatternsTests do "eggs, milk", "Honda", MatchError, + {:multiple, [:make, "Honda"]}, [1,2,3], {:multiple, ["Meow", "Woof", "Eh?"]}, {:multiple, ["Mickey", "Donald", "I need a name!"]},