From 5e9e5e3891af64049d7070f03fc11d4ccc73ba75 Mon Sep 17 00:00:00 2001 From: Mahmut Surekci Date: Thu, 26 May 2016 02:02:20 +0100 Subject: [PATCH 1/5] Adding new koans for map sets --- lib/koans/17_map_sets.ex | 62 ++++++++++++++++++++++++++++++ lib/runner.ex | 1 + test/koans/map_sets_koans_test.exs | 21 ++++++++++ 3 files changed, 84 insertions(+) create mode 100644 lib/koans/17_map_sets.ex create mode 100644 test/koans/map_sets_koans_test.exs diff --git a/lib/koans/17_map_sets.ex b/lib/koans/17_map_sets.ex new file mode 100644 index 0000000..28b4dc5 --- /dev/null +++ b/lib/koans/17_map_sets.ex @@ -0,0 +1,62 @@ +defmodule MapSets do + use Koans + + @intro "Map sets" + + @set MapSet.new([1, 2, 3, 4, 5]) + + koan "Does this value exist in the map set?" do + assert MapSet.member?(@set, 3) == ___ + end + + koan "Multiply everything by 3 in my set" do + new_set = MapSet.new(@set, fn x -> 3 * x end) + + assert MapSet.member?(new_set, 15) == ___ + assert MapSet.member?(new_set, 1) == ___ + end + + koan "Add this value into a map set" do + modified_set = MapSet.put(@set, 6) + + assert MapSet.member?(modified_set, 6) == ___ + end + + koan "Delete this value from the map set" do + modified_set = MapSet.delete(@set, 1) + + assert MapSet.member?(modified_set, 1) == ___ + end + + koan "How large is my map set?" do + assert MapSet.size(@set) == ___ + end + + koan "Are these maps twins?" do + new_set = MapSet.new([1, 2, 3]) + + assert MapSet.equal?(@set, new_set) == ___ + end + + koan "I want only the common values in both sets" do + intersection_set = MapSet.intersection(@set, MapSet.new([5, 6, 7])) + + assert MapSet.member?(intersection_set, 5) == ___ + end + + koan "Duplication is a no-no" do + new_set = MapSet.new([1, 1, 2, 3, 3]) + + assert MapSet.size(new_set) == ___ + end + + koan "Unify my sets" do + new_set = MapSet.union(@set, MapSet.new([1, 5, 6, 7])) + + assert MapSet.size(new_set) == ___ + end + + koan "I want my set in a list" do + assert MapSet.to_list(@set) == ___ + end +end \ No newline at end of file diff --git a/lib/runner.ex b/lib/runner.ex index 401db50..98cc08b 100644 --- a/lib/runner.ex +++ b/lib/runner.ex @@ -15,6 +15,7 @@ defmodule Runner do Processes, Tasks, Agents, + MapSets, ] def koan?(koan), do: Enum.member?(@modules, koan) diff --git a/test/koans/map_sets_koans_test.exs b/test/koans/map_sets_koans_test.exs new file mode 100644 index 0000000..71b32e8 --- /dev/null +++ b/test/koans/map_sets_koans_test.exs @@ -0,0 +1,21 @@ +defmodule MapSetsTest do + use ExUnit.Case + import TestHarness + + test "MapSets" do + answers = [ + true, + {:multiple, [true, false]}, + true, + false, + 5, + false, + true, + 3, + 7, + [1, 2, 3, 4, 5], + ] + + test_all(MapSets, answers) + end +end \ No newline at end of file From 317ccc790f142a97cc20a512ae133c29e7449822 Mon Sep 17 00:00:00 2001 From: Mahmut Surekci Date: Thu, 26 May 2016 18:48:46 +0100 Subject: [PATCH 2/5] James Bond reference in the intro title --- lib/koans/17_map_sets.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/koans/17_map_sets.ex b/lib/koans/17_map_sets.ex index 28b4dc5..41ed908 100644 --- a/lib/koans/17_map_sets.ex +++ b/lib/koans/17_map_sets.ex @@ -1,7 +1,7 @@ defmodule MapSets do use Koans - @intro "Map sets" + @intro "My name is Set, MapSet" @set MapSet.new([1, 2, 3, 4, 5]) From 317ea5d2083f5c19a1a45b5c4d89c0f9a7ef9a11 Mon Sep 17 00:00:00 2001 From: Mahmut Surekci Date: Fri, 27 May 2016 15:51:45 +0100 Subject: [PATCH 3/5] Reposition MapSets koans. Reposition some of the koans to make it clear why MapSets are interesting. --- lib/koans/{17_map_sets.ex => 10_map_sets.ex} | 24 +++++++++---------- lib/koans/{10_structs.ex => 11_structs.ex} | 0 ...ern_matching.ex => 12_pattern_matching.ex} | 0 .../{12_functions.ex => 13_functions.ex} | 0 lib/koans/{13_enums.ex => 14_enums.ex} | 0 .../{14_processes.ex => 15_processes.ex} | 0 lib/koans/{15_tasks.ex => 16_tasks.ex} | 0 lib/koans/{16_agents.ex => 17_agents.ex} | 0 .../{16_protocols.ex => 18_protocols.ex} | 0 lib/runner.ex | 2 +- 10 files changed, 13 insertions(+), 13 deletions(-) rename lib/koans/{17_map_sets.ex => 10_map_sets.ex} (85%) rename lib/koans/{10_structs.ex => 11_structs.ex} (100%) rename lib/koans/{11_pattern_matching.ex => 12_pattern_matching.ex} (100%) rename lib/koans/{12_functions.ex => 13_functions.ex} (100%) rename lib/koans/{13_enums.ex => 14_enums.ex} (100%) rename lib/koans/{14_processes.ex => 15_processes.ex} (100%) rename lib/koans/{15_tasks.ex => 16_tasks.ex} (100%) rename lib/koans/{16_agents.ex => 17_agents.ex} (100%) rename lib/koans/{16_protocols.ex => 18_protocols.ex} (100%) diff --git a/lib/koans/17_map_sets.ex b/lib/koans/10_map_sets.ex similarity index 85% rename from lib/koans/17_map_sets.ex rename to lib/koans/10_map_sets.ex index 41ed908..6777150 100644 --- a/lib/koans/17_map_sets.ex +++ b/lib/koans/10_map_sets.ex @@ -1,21 +1,27 @@ defmodule MapSets do use Koans - @intro "My name is Set, MapSet" + @intro "My name is Set, MapSet." @set MapSet.new([1, 2, 3, 4, 5]) - koan "Does this value exist in the map set?" do - assert MapSet.member?(@set, 3) == ___ - end - - koan "Multiply everything by 3 in my set" do + koan "I am merely another collection but, you can perform some operations on me" do new_set = MapSet.new(@set, fn x -> 3 * x end) assert MapSet.member?(new_set, 15) == ___ assert MapSet.member?(new_set, 1) == ___ end + koan "However, I do not allow duplication" do + new_set = MapSet.new([1, 1, 2, 3, 3, 3]) + + assert MapSet.size(new_set) == ___ + end + + koan "Does this value exist in the map set?" do + assert MapSet.member?(@set, 3) == ___ + end + koan "Add this value into a map set" do modified_set = MapSet.put(@set, 6) @@ -44,12 +50,6 @@ defmodule MapSets do assert MapSet.member?(intersection_set, 5) == ___ end - koan "Duplication is a no-no" do - new_set = MapSet.new([1, 1, 2, 3, 3]) - - assert MapSet.size(new_set) == ___ - end - koan "Unify my sets" do new_set = MapSet.union(@set, MapSet.new([1, 5, 6, 7])) diff --git a/lib/koans/10_structs.ex b/lib/koans/11_structs.ex similarity index 100% rename from lib/koans/10_structs.ex rename to lib/koans/11_structs.ex diff --git a/lib/koans/11_pattern_matching.ex b/lib/koans/12_pattern_matching.ex similarity index 100% rename from lib/koans/11_pattern_matching.ex rename to lib/koans/12_pattern_matching.ex diff --git a/lib/koans/12_functions.ex b/lib/koans/13_functions.ex similarity index 100% rename from lib/koans/12_functions.ex rename to lib/koans/13_functions.ex diff --git a/lib/koans/13_enums.ex b/lib/koans/14_enums.ex similarity index 100% rename from lib/koans/13_enums.ex rename to lib/koans/14_enums.ex diff --git a/lib/koans/14_processes.ex b/lib/koans/15_processes.ex similarity index 100% rename from lib/koans/14_processes.ex rename to lib/koans/15_processes.ex diff --git a/lib/koans/15_tasks.ex b/lib/koans/16_tasks.ex similarity index 100% rename from lib/koans/15_tasks.ex rename to lib/koans/16_tasks.ex diff --git a/lib/koans/16_agents.ex b/lib/koans/17_agents.ex similarity index 100% rename from lib/koans/16_agents.ex rename to lib/koans/17_agents.ex diff --git a/lib/koans/16_protocols.ex b/lib/koans/18_protocols.ex similarity index 100% rename from lib/koans/16_protocols.ex rename to lib/koans/18_protocols.ex diff --git a/lib/runner.ex b/lib/runner.ex index 35bab7c..ac87ae5 100644 --- a/lib/runner.ex +++ b/lib/runner.ex @@ -9,6 +9,7 @@ defmodule Runner do Lists, KeywordLists, Maps, + MapSets, Structs, PatternMatching, Functions, @@ -17,7 +18,6 @@ defmodule Runner do Tasks, Agents, Protocols, - MapSets, ] def koan?(koan), do: Enum.member?(@modules, koan) From 925f6ba2f764c1579adb86da5da7eb20486c4c30 Mon Sep 17 00:00:00 2001 From: Mahmut Surekci Date: Fri, 27 May 2016 15:54:48 +0100 Subject: [PATCH 4/5] Change order of answers to make unit tests pass --- test/koans/map_sets_koans_test.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/koans/map_sets_koans_test.exs b/test/koans/map_sets_koans_test.exs index 71b32e8..0a200ee 100644 --- a/test/koans/map_sets_koans_test.exs +++ b/test/koans/map_sets_koans_test.exs @@ -4,14 +4,14 @@ defmodule MapSetsTest do test "MapSets" do answers = [ - true, {:multiple, [true, false]}, + 3, + true, true, false, 5, false, true, - 3, 7, [1, 2, 3, 4, 5], ] From 48df8ad8afc577a79b8f681fa64c999e5aae6701 Mon Sep 17 00:00:00 2001 From: Mahmut Surekci Date: Fri, 27 May 2016 18:44:47 +0100 Subject: [PATCH 5/5] Demonstrating MapSets similarity to List using Enum.fetch. Koan added to demonstrate MapSets being unordered after 32. --- lib/koans/10_map_sets.ex | 20 ++++++++++++++------ test/koans/map_sets_koans_test.exs | 4 +++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/koans/10_map_sets.ex b/lib/koans/10_map_sets.ex index 6777150..353e8b7 100644 --- a/lib/koans/10_map_sets.ex +++ b/lib/koans/10_map_sets.ex @@ -5,12 +5,9 @@ defmodule MapSets do @set MapSet.new([1, 2, 3, 4, 5]) - koan "I am merely another collection but, you can perform some operations on me" do - new_set = MapSet.new(@set, fn x -> 3 * x end) - - assert MapSet.member?(new_set, 15) == ___ - assert MapSet.member?(new_set, 1) == ___ - end + koan "I am very similar to a list" do + assert Enum.fetch(@set, 0) == {:ok, ___} + end koan "However, I do not allow duplication" do new_set = MapSet.new([1, 1, 2, 3, 3, 3]) @@ -18,6 +15,17 @@ defmodule MapSets do assert MapSet.size(new_set) == ___ end + koan "Even though I am like a list I am unordered after 32 elements" do + assert 1..33 |> MapSet.new() |> Enum.fetch(0) == {___, 11} + end + + koan "I am merely another collection but, you can perform some operations on me" do + new_set = MapSet.new(@set, fn x -> 3 * x end) + + assert MapSet.member?(new_set, 15) == ___ + assert MapSet.member?(new_set, 1) == ___ + end + koan "Does this value exist in the map set?" do assert MapSet.member?(@set, 3) == ___ end diff --git a/test/koans/map_sets_koans_test.exs b/test/koans/map_sets_koans_test.exs index 0a200ee..2bb25ea 100644 --- a/test/koans/map_sets_koans_test.exs +++ b/test/koans/map_sets_koans_test.exs @@ -4,8 +4,10 @@ defmodule MapSetsTest do test "MapSets" do answers = [ - {:multiple, [true, false]}, + 1, 3, + :ok, + {:multiple, [true, false]}, true, true, false,