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 function koans
  • Loading branch information
iamvery committed Sep 1, 2025
commit f637dff7c306461b4cb5650a105e7e9c95ae7ebf
34 changes: 34 additions & 0 deletions lib/koans/13_functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ defmodule Functions do
assert result == ___
end

koan "Pipes make data transformation pipelines readable" do
numbers = [1, 2, 3, 4, 5]

result =
numbers
|> Enum.filter(&(&1 > 2))
|> Enum.map(&(&1 * 2))
|> Enum.sum()

assert result == ___

user_input = " Hello World "

cleaned =
user_input
|> String.trim()
|> String.downcase()
|> String.replace(" ", "_")

assert cleaned == ___
end

koan "Conveniently keyword lists can be used for function options" do
transform = fn str, opts ->
if opts[:upcase] do
Expand All @@ -122,4 +144,16 @@ defmodule Functions do
assert transform.("good", upcase: true) == ___
assert transform.("good", upcase: false) == ___
end

koan "Anonymous functions can use the & capture syntax for very concise definitions" do
add_one = &(&1 + 1)
multiply_by_two = &(&1 * 2)

result = 5 |> add_one.() |> multiply_by_two.()
assert result == ___

# You can also capture existing functions
string_length = &String.length/1
assert string_length.("hello") == ___
end
end
4 changes: 3 additions & 1 deletion test/koans/functions_koans_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ defmodule FunctionsTests do
100,
1000,
"Full Name",
{:multiple, ["GOOD", "good"]}
{:multiple, [24, "hello_world"]},
{:multiple, ["GOOD", "good"]},
{:multiple, [12, 5]}
]

test_all(Functions, answers)
Expand Down