Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update and extend enum koans
  • Loading branch information
iamvery committed Sep 1, 2025
commit 767b9f65dbc4667f424062333fc982bedbf9401d
52 changes: 48 additions & 4 deletions lib/koans/14_enums.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ defmodule Enums do
assert Enum.count([1, 2, 3]) == ___
end

koan "Depending on the type, it counts pairs" do
assert Enum.count(%{a: :foo, b: :bar}) == ___
koan "Counting is similar to length" do
assert length([1, 2, 3]) == ___
end

koan "But it allows you to count certain elements" do
assert Enum.count([1, 2, 3], &(&1 == 2)) == ___
end

koan "Depending on the type, it counts pairs while length does not" do
map = %{a: :foo, b: :bar}
assert Enum.count(map) == ___
assert_raise ___, fn -> length(map) end
end

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

def multiply_by_ten(n), do: 10 * n

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

Expand Down Expand Up @@ -66,7 +76,7 @@ defmodule Enums do
assert Enum.zip(letters, numbers) == ___
end

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

Expand All @@ -83,4 +93,38 @@ defmodule Enums do
koan "Collapse an entire list of elements down to a single one by repeating a function." do
assert Enum.reduce([1, 2, 3], 0, fn element, accumulator -> element + accumulator end) == ___
end

koan "Enum.chunk_every splits lists into smaller lists of fixed size" do
assert Enum.chunk_every([1, 2, 3, 4, 5, 6], 2) == ___
assert Enum.chunk_every([1, 2, 3, 4, 5], 3) == ___
end

koan "Enum.flat_map transforms and flattens in one step" do
result =
[1, 2, 3]
|> Enum.flat_map(&[&1, &1 * 10])

assert result == ___
end

koan "Enum.group_by organizes elements by a grouping function" do
words = ["apple", "banana", "cherry", "apricot", "blueberry"]
grouped = Enum.group_by(words, &String.first/1)

assert grouped["a"] == ___
assert grouped["b"] == ___
end

koan "Stream provides lazy enumeration for large datasets" do
# Streams are lazy - they don't execute until you call Enum on them
stream =
1..1_000_000
|> Stream.filter(&even?/1)
|> Stream.map(&(&1 * 2))
|> Stream.take(3)

# Nothing has been computed yet!
result = Enum.to_list(stream)
assert result == ___
end
end
10 changes: 8 additions & 2 deletions test/koans/enum_koans_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ defmodule EnumTests do
test "Enums" do
answers = [
3,
2,
3,
1,
{:multiple, [2, ArgumentError]},
{:multiple, [true, false]},
{:multiple, [true, false]},
{:multiple, [true, false]},
Expand All @@ -19,7 +21,11 @@ defmodule EnumTests do
2,
nil,
:no_such_element,
6
6,
{:multiple, [[[1, 2], [3, 4], [5, 6]], [[1, 2, 3], [4, 5]]]},
[1, 10, 2, 20, 3, 30],
{:multiple, [["apple", "apricot"], ["banana", "blueberry"]]},
[4, 8, 12]
]

test_all(Enums, answers)
Expand Down