-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathabout_processes.exs
65 lines (51 loc) · 1.3 KB
/
about_processes.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
defmodule AboutProcesses do
use Koans
think "Spawning a process executes a function" do
spawn __?
# Hint: Print something to the screen so you know something happened!
end
think "Spawning a process returns a process ID (PID)" do
pid = spawn fn -> IO.puts "I am running in another process" end
assert is_pid(pid) == __?
end
think "You are a process" do
assert_? is_pid(self)
end
think "Processes send and receive messages; it's like mailbox" do
send self, {:hello, "world"}
receive do
{:hello, message} -> assert message == __?
end
end
think "Processes communicate with one another" do
echo = fn ->
receive do
{caller, value} -> send caller, value
end
end
pid = spawn echo
send pid, {self, "hi!"}
receive do
value -> assert value == __?
end
end
def echo_loop do
receive do
{caller, value} ->
send caller, value
echo_loop
end
end
think "Use tail recursion (calling a function as the very last statement) to receive multiple messages" do
pid = spawn &echo_loop/0
send pid, {self, "o"}
receive do
value -> assert value == __?
end
send pid, {self, "hai"}
receive do
value -> assert value == __?
end
Process.exit(pid, :kill)
end
end