概要
paiza.ioでelixirやってみた。
Scheduler調べてみた。
サンプルコード
defmodule FibSolver do
def fib(scheduler) do
send scheduler, {:ready, self()}
receive do
{:fib, n, client} ->
IO.puts "ok1"
send client, {:answer, n, fib_calc(n), self()}
fib(scheduler)
{:shutdown} ->
IO.puts "ok3"
exit(0)
end
end
defp fib_calc(0), do: 1
defp fib_calc(1), do: 1
defp fib_calc(n), do: fib_calc(n - 1) + fib_calc(n - 2)
end
Enum.each 1..3, fn n ->
IO.puts n
end
(1..3)
|> Enum.map(fn(n) ->
IO.puts n
end)
spawn(FibSolver, :fib, [self()])
receive do
{:ready, pid} ->
IO.puts "ok0"
send pid, {:fib, 9, self()}
#{:answer, number, result, _pid} ->
# IO.puts "ok1"
end
receive do
#{:ready, pid} ->
# IO.puts "ok0"
# send pid, {:fib, 9, self()}
{:answer, number, result, _pid} ->
IO.puts "ok2"
IO.puts result
send _pid, {:shutdown}
end
実行結果
1
2
3
1
2
3
ok0
ok1
ok2
55
ok3
成果物
以上。