Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ゆとりAdventCalendar用
$ erl
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.1 (abort with ^G)
1> c(yutori).
{ok,yutori}
2> yutori:call().
true
3> yutori:sushi(6,10).
sushi
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
<0.42.0> sushi.
<0.43.0> sushi.
<0.44.0> sushi.
<0.45.0> sushi.
<0.46.0> sushi.
<0.40.0> sushi.
$ erl
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.1 (abort with ^G)
1> l(yutori).
{module,yutori}
2> yutori:sushi(6,10).
** exception throw: {error,yutori_nante_nakatta}
in function yutori:send/3 (yutori.erl, line 19)
-module(yutori).
-import(io, [format/2]).
-export([waiting/0, call/0, sushi/2, prepare/3, send/3, stop/0]).
call() ->
register(yutori_is_waiting, spawn(yutori, waiting, [])).
stop() ->
case whereis(yutori_is_waiting) of
undefined -> throw({error, yutori_nante_nakatta});
Pid -> Pid ! {stop_process, self()}
end.
sushi(Number, Times) ->
send(Number, Times, sushi).
send(Processes, Times, Message) when is_number(Processes), is_number(Times), Processes > 1, Message > 0 ->
case whereis(yutori_is_waiting) of
undefined -> throw({error, yutori_nante_nakatta});
Pid -> Pid ! {send_message, Processes, Times, Message},
Message
end;
send(_, _, _) ->
error(badarg).
waiting() ->
receive
{send_message, Processes, Times, Message} ->
{Process, Time} = {Processes - 1, Times - 1},
Next = spawn(yutori, prepare, [Process, Time, self()]),
Next ! {message, Message},
loop(Time, Next),
waiting();
{stop_process, Pid} ->
format("yutori~p is going to stop by ~p's order.~n", [self(), Pid]),
ok;
_ ->
try throw({error, "unexpected message comes!"}) of
_ -> waiting()
catch
Type:Message -> format("~p~n~n", [{Type, Message}]),
waiting()
end
end.
prepare(Process, Times, Top) ->
Next = next(Process - 1, Times, Top),
loop(Times, Next).
next(0, _, Top) ->
Top;
next(Process, Times, Top) ->
spawn(yutori, prepare, [Process, Times, Top]).
loop(Times, Next) ->
receive
{message, Msg} ->
timer:sleep(100),
format("~p ~p.~n", [self(), Msg]),
Next ! {message, Msg},
case Times > 0 of
true -> loop(Times - 1, Next);
false -> finished
end
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment