Add koan illustrating tail recursion

This commit is contained in:
Jay Hayes
2016-05-03 17:48:21 -05:00
parent 1f24de98f4
commit 6126d5a836
2 changed files with 26 additions and 0 deletions

View File

@@ -57,6 +57,31 @@ defmodule Processes do
assert_receive ___
end
def yelling_echo_loop do
receive do
{caller, value} ->
send caller, String.upcase(value)
yelling_echo_loop
end
end
koan "Use tail recursion (calling a function as the very last statement) to receive multiple messages" do
pid = spawn &yelling_echo_loop/0
send pid, {self, "o"}
send pid, {self, "hai"}
receive do
msg -> assert msg == ___
end
receive do
msg -> assert msg == ___
end
Process.exit(pid, :kill)
end
koan "Waiting for a message can get boring" do
parent = self
spawn(fn -> receive do