Add koan illustrating tail recursion
This commit is contained in:
@@ -57,6 +57,31 @@ defmodule Processes do
|
|||||||
assert_receive ___
|
assert_receive ___
|
||||||
end
|
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
|
koan "Waiting for a message can get boring" do
|
||||||
parent = self
|
parent = self
|
||||||
spawn(fn -> receive do
|
spawn(fn -> receive do
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ defmodule ProcessesTests do
|
|||||||
true,
|
true,
|
||||||
"hola!",
|
"hola!",
|
||||||
:how_are_you?,
|
:how_are_you?,
|
||||||
|
{:multiple, ["O", "HAI"]},
|
||||||
{:waited_too_long, "I am impatient"},
|
{:waited_too_long, "I am impatient"},
|
||||||
{:exited, :random_reason},
|
{:exited, :random_reason},
|
||||||
:normal,
|
:normal,
|
||||||
|
|||||||
Reference in New Issue
Block a user