Skip to content

Commit 3975e54

Browse files
committed
Update and extend enum koans
1 parent 9ffe0f8 commit 3975e54

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

lib/koans/14_enums.ex

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ defmodule Enums do
88
assert Enum.count([1, 2, 3]) == ___
99
end
1010

11-
koan "Depending on the type, it counts pairs" do
12-
assert Enum.count(%{a: :foo, b: :bar}) == ___
11+
koan "Counting is similar to length" do
12+
assert length([1, 2, 3]) == ___
13+
end
14+
15+
koan "But it allows you to count certain elements" do
16+
assert Enum.count([1, 2, 3], &(&1 == 2)) == ___
17+
end
18+
19+
koan "Depending on the type, it counts pairs while length does not" do
20+
map = %{a: :foo, b: :bar}
21+
assert Enum.count(map) == ___
22+
assert_raise ___, fn -> length(map) end
1323
end
1424

1525
def less_than_five?(n), do: n < 5
@@ -34,7 +44,7 @@ defmodule Enums do
3444

3545
def multiply_by_ten(n), do: 10 * n
3646

37-
koan "Map converts each element of a list by running some function with it" do
47+
koan "Mapping converts each element of a list by running some function with it" do
3848
assert Enum.map([1, 2, 3], &multiply_by_ten/1) == ___
3949
end
4050

@@ -66,7 +76,7 @@ defmodule Enums do
6676
assert Enum.zip(letters, numbers) == ___
6777
end
6878

69-
koan "When you want to find that one pesky element" do
79+
koan "When you want to find that one pesky element, it returns the first" do
7080
assert Enum.find([1, 2, 3, 4], &even?/1) == ___
7181
end
7282

@@ -83,4 +93,38 @@ defmodule Enums do
8393
koan "Collapse an entire list of elements down to a single one by repeating a function." do
8494
assert Enum.reduce([1, 2, 3], 0, fn element, accumulator -> element + accumulator end) == ___
8595
end
96+
97+
koan "Enum.chunk_every splits lists into smaller lists of fixed size" do
98+
assert Enum.chunk_every([1, 2, 3, 4, 5, 6], 2) == ___
99+
assert Enum.chunk_every([1, 2, 3, 4, 5], 3) == ___
100+
end
101+
102+
koan "Enum.flat_map transforms and flattens in one step" do
103+
result =
104+
[1, 2, 3]
105+
|> Enum.flat_map(&[&1, &1 * 10])
106+
107+
assert result == ___
108+
end
109+
110+
koan "Enum.group_by organizes elements by a grouping function" do
111+
words = ["apple", "banana", "cherry", "apricot", "blueberry"]
112+
grouped = Enum.group_by(words, &String.first/1)
113+
114+
assert grouped["a"] == ___
115+
assert grouped["b"] == ___
116+
end
117+
118+
koan "Stream provides lazy enumeration for large datasets" do
119+
# Streams are lazy - they don't execute until you call Enum on them
120+
stream =
121+
1..1_000_000
122+
|> Stream.filter(&even?/1)
123+
|> Stream.map(&(&1 * 2))
124+
|> Stream.take(3)
125+
126+
# Nothing has been computed yet!
127+
result = Enum.to_list(stream)
128+
assert result == ___
129+
end
86130
end

test/koans/enum_koans_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ defmodule EnumTests do
55
test "Enums" do
66
answers = [
77
3,
8-
2,
8+
3,
9+
1,
10+
{:multiple, [2, ArgumentError]},
911
{:multiple, [true, false]},
1012
{:multiple, [true, false]},
1113
{:multiple, [true, false]},
@@ -19,7 +21,11 @@ defmodule EnumTests do
1921
2,
2022
nil,
2123
:no_such_element,
22-
6
24+
6,
25+
{:multiple, [[[1, 2], [3, 4], [5, 6]], [[1, 2, 3], [4, 5]]]},
26+
[1, 10, 2, 20, 3, 30],
27+
{:multiple, [["apple", "apricot"], ["banana", "blueberry"]]},
28+
[4, 8, 12]
2329
]
2430

2531
test_all(Enums, answers)

0 commit comments

Comments
 (0)