From 92973a0fb750577f97b4e18dc9e7cb1e2120e1f9 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Wed, 4 May 2016 19:22:18 -0500 Subject: [PATCH 1/4] Add keyword list koan at after maps --- lib/koans/06_keyword_lists.ex | 0 lib/koans/{06_maps.ex => 07_maps.ex} | 0 lib/koans/{07_structs.ex => 08_structs.ex} | 0 lib/koans/{08_pattern_matching.ex => 09_pattern_matching.ex} | 0 lib/koans/{09_functions.ex => 10_functions.ex} | 0 lib/koans/{10_enums.ex => 11_enums.ex} | 0 lib/koans/{11_processes.ex => 12_processes.ex} | 0 lib/koans/{12_tasks.ex => 13_tasks.ex} | 0 lib/koans/{13_agents.ex => 14_agents.ex} | 0 9 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/koans/06_keyword_lists.ex rename lib/koans/{06_maps.ex => 07_maps.ex} (100%) rename lib/koans/{07_structs.ex => 08_structs.ex} (100%) rename lib/koans/{08_pattern_matching.ex => 09_pattern_matching.ex} (100%) rename lib/koans/{09_functions.ex => 10_functions.ex} (100%) rename lib/koans/{10_enums.ex => 11_enums.ex} (100%) rename lib/koans/{11_processes.ex => 12_processes.ex} (100%) rename lib/koans/{12_tasks.ex => 13_tasks.ex} (100%) rename lib/koans/{13_agents.ex => 14_agents.ex} (100%) diff --git a/lib/koans/06_keyword_lists.ex b/lib/koans/06_keyword_lists.ex new file mode 100644 index 0000000..e69de29 diff --git a/lib/koans/06_maps.ex b/lib/koans/07_maps.ex similarity index 100% rename from lib/koans/06_maps.ex rename to lib/koans/07_maps.ex diff --git a/lib/koans/07_structs.ex b/lib/koans/08_structs.ex similarity index 100% rename from lib/koans/07_structs.ex rename to lib/koans/08_structs.ex diff --git a/lib/koans/08_pattern_matching.ex b/lib/koans/09_pattern_matching.ex similarity index 100% rename from lib/koans/08_pattern_matching.ex rename to lib/koans/09_pattern_matching.ex diff --git a/lib/koans/09_functions.ex b/lib/koans/10_functions.ex similarity index 100% rename from lib/koans/09_functions.ex rename to lib/koans/10_functions.ex diff --git a/lib/koans/10_enums.ex b/lib/koans/11_enums.ex similarity index 100% rename from lib/koans/10_enums.ex rename to lib/koans/11_enums.ex diff --git a/lib/koans/11_processes.ex b/lib/koans/12_processes.ex similarity index 100% rename from lib/koans/11_processes.ex rename to lib/koans/12_processes.ex diff --git a/lib/koans/12_tasks.ex b/lib/koans/13_tasks.ex similarity index 100% rename from lib/koans/12_tasks.ex rename to lib/koans/13_tasks.ex diff --git a/lib/koans/13_agents.ex b/lib/koans/14_agents.ex similarity index 100% rename from lib/koans/13_agents.ex rename to lib/koans/14_agents.ex From b45a08135aa0f14a8db3e5cb5d621a00016a48f7 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Wed, 4 May 2016 19:28:24 -0500 Subject: [PATCH 2/4] Add keyword lists koan --- lib/koans/06_keyword_lists.ex | 62 +++++++++++++++++++++++++ test/koans/keyword_lists_koans_test.exs | 19 ++++++++ 2 files changed, 81 insertions(+) create mode 100644 test/koans/keyword_lists_koans_test.exs diff --git a/lib/koans/06_keyword_lists.ex b/lib/koans/06_keyword_lists.ex index e69de29..20ebda2 100644 --- a/lib/koans/06_keyword_lists.ex +++ b/lib/koans/06_keyword_lists.ex @@ -0,0 +1,62 @@ +defmodule KeywordLists do + use Koans + + 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 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) + end + + assert transform.("good", upcase: true) == ___ + end + + def foo(kw_list), do: kw_list + + koan "Actually function bodies are a sneaky use of keyword lists" do + list = foo do + :good + end + + assert list == [do: ___] + end + + def iff(kw_list), do: kw_list + + koan "Turns out our beloved if statements are also using keyword lists" do + list = iff do + :this + else + :that + end + + assert list == [do: ___, else: ___] + end +end diff --git a/test/koans/keyword_lists_koans_test.exs b/test/koans/keyword_lists_koans_test.exs new file mode 100644 index 0000000..d02c605 --- /dev/null +++ b/test/koans/keyword_lists_koans_test.exs @@ -0,0 +1,19 @@ +defmodule KeywordListsTests do + use ExUnit.Case + import TestHarness + + test "KeywordLists" do + answers = [ + "bar", + "bar", + "baz", + {:multiple, [:foo, "bar"]}, + "foo", + "GOOD", + :good, + {:multiple, [:this, :that]}, + ] + + test_all(KeywordLists, answers) + end +end From 27b8926aaa32621bdf6a34596ff9a42cb5d7d65c Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Thu, 5 May 2016 17:57:22 -0500 Subject: [PATCH 3/4] Add keyword lists koan to runner --- lib/runner.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/runner.ex b/lib/runner.ex index a780289..aff090a 100644 --- a/lib/runner.ex +++ b/lib/runner.ex @@ -5,6 +5,7 @@ defmodule Runner do Atoms, Tuples, Lists, + KeywordLists, Maps, Structs, PatternMatching, From 1c182a2c8c8c31836c80274253d8efa32fd1371e Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Thu, 5 May 2016 18:12:15 -0500 Subject: [PATCH 4/4] Remove metaprogramming-related keyword list koans Too advanced at this point. Will considering including something like this in #21. --- lib/koans/06_keyword_lists.ex | 22 ---------------------- test/koans/keyword_lists_koans_test.exs | 2 -- 2 files changed, 24 deletions(-) diff --git a/lib/koans/06_keyword_lists.ex b/lib/koans/06_keyword_lists.ex index 20ebda2..38d0039 100644 --- a/lib/koans/06_keyword_lists.ex +++ b/lib/koans/06_keyword_lists.ex @@ -37,26 +37,4 @@ defmodule KeywordLists do assert transform.("good", upcase: true) == ___ end - - def foo(kw_list), do: kw_list - - koan "Actually function bodies are a sneaky use of keyword lists" do - list = foo do - :good - end - - assert list == [do: ___] - end - - def iff(kw_list), do: kw_list - - koan "Turns out our beloved if statements are also using keyword lists" do - list = iff do - :this - else - :that - end - - assert list == [do: ___, else: ___] - end end diff --git a/test/koans/keyword_lists_koans_test.exs b/test/koans/keyword_lists_koans_test.exs index d02c605..617ba5a 100644 --- a/test/koans/keyword_lists_koans_test.exs +++ b/test/koans/keyword_lists_koans_test.exs @@ -10,8 +10,6 @@ defmodule KeywordListsTests do {:multiple, [:foo, "bar"]}, "foo", "GOOD", - :good, - {:multiple, [:this, :that]}, ] test_all(KeywordLists, answers)