Skip to content

Instantly share code, notes, and snippets.

@nekketsuuu
Last active December 30, 2015 06:02
Show Gist options
  • Save nekketsuuu/e4d8ca2a5695e99d3be2 to your computer and use it in GitHub Desktop.
Save nekketsuuu/e4d8ca2a5695e99d3be2 to your computer and use it in GitHub Desktop.
*~
*.beam
*.dump
-module(conc1).
-export([start/1, ping/1, pong/0]).
ping(0) ->
pong ! finished,
io:format("ping finished~n", []);
ping(M) ->
io:format("ping ~p~n", [M]),
pong ! {ping, self()},
receive
pong ->
received
end,
ping(M-1).
pong() ->
receive
finished ->
io:format("pong finished~n", []);
{ping, PID} ->
PID ! pong,
io:format("pong~n", []),
pong()
end.
start(M) ->
register(pong, spawn(conc1, pong, [])),
spawn(conc1, ping, [M]).
-module(conc2).
-export([start/2, proc/2]).
proc(leader, Send_PID) ->
receive
{set_pid, PID} ->
proc(leader, PID);
{hi, 0} ->
Send_PID ! finished,
proc(leader, Send_PID);
{hi, M} ->
io:format("hi to ~p (~p; leader)~n", [Send_PID, self()]),
Send_PID ! {hi, M-1},
proc(leader, Send_PID);
finished ->
io:format("~p (leader) finished~n", [self()])
end;
proc(others, Send_PID) ->
receive
{hi, M} ->
io:format("hi to ~p (~p)~n", [Send_PID, self()]),
Send_PID ! {hi, M},
proc(others, Send_PID);
finished ->
Send_PID ! finished,
io:format("~p finished~n", [self()])
end.
spawn_ring(N) when N =< 1 ->
exit('# of processes should be greater than 1');
spawn_ring(N) ->
Leader_PID = spawn(conc2, proc, [leader, self()]), % self() for dummy PID
Last_PID = spawn_ring(N-1, Leader_PID),
Leader_PID ! {set_pid, Last_PID},
io:format("% spawn_ring #~p (leader): My_PID = ~p, Send_PID = ~p~n", [N, Leader_PID, Last_PID]),
io:format("spawn finished~n", []),
Leader_PID.
spawn_ring(1, Send_PID) ->
Last_PID = spawn(conc2, proc, [others, Send_PID]),
io:format("% spawn_ring #~p: My_PID = ~p, Send_PID = ~p~n", [1, Last_PID, Send_PID]),
Last_PID;
spawn_ring(N, Send_PID) ->
My_PID = spawn(conc2, proc, [others, Send_PID]),
io:format("% spawn_ring #~p: My_PID = ~p, Send_PID = ~p~n", [N, My_PID, Send_PID]),
spawn_ring(N-1, My_PID).
start(M, N) ->
Leader_PID = spawn_ring(N),
Leader_PID ! {hi, M},
start_end.
% Type "i()." in order to know that all processes terminate correctly.
-module(conc3).
-export([start/2, proc_center/1, proc_edge/1]).
%% 自分を除いてN個のプロセッサとM回通信
%% 通信終了がN回起こったら自分も終了
%% (別々のM個が終了したのかどうかは気にしないことにする)
%% (process dictionaryを使うと別の書き方できるかも?)
proc_center(0) ->
io:format("center finished~n", []);
proc_center(N) ->
receive
{ping, Pong_PID} ->
io:format("pong to ~p~n", [Pong_PID]),
Pong_PID ! pong,
proc_center(N);
finished ->
proc_center(N-1)
end.
proc_edge(0) ->
center ! finished,
io:format("edge ~p finished~n", [self()]);
proc_edge(M) ->
io:format("ping from ~p~n", [self()]),
center ! {ping, self()},
receive
pong ->
proc_edge(M-1)
end.
spawn_edge(0, _) ->
spawn_edge_finished;
spawn_edge(M, N) ->
spawn(conc3, proc_edge, [M]),
spawn_edge(N-1, M).
start(M, N) ->
register(center, spawn(conc3, proc_center, [N-1])),
spawn_edge(N-1, M).
-module(demo).
-export([double/1]).
double(N) ->
N * 2.
-module(lists1).
-export([min/1, max/1, min_max/1]).
-compile({no_auto_import, [min/2, max/2]}).
min([Head|Rest]) ->
min(Rest, Head).
min([], Min) ->
Min;
min([Head|Rest], CurrentMin) ->
min(Rest, erlang:min(Head, CurrentMin)).
max([Head|Rest]) ->
max(Rest, Head).
max([], Max) ->
Max;
max([Head|Rest], CurrentMax) ->
max(Rest, erlang:max(Head, CurrentMax)).
min_max(List) ->
{min(List), max(List)}.
-module(mathStuff).
-export([perimeter/1]).
perimeter({square, Side}) ->
4 * Side;
perimeter({circle, Radius}) ->
2 * math:pi() * Radius;
perimeter({triangle, A, B, C}) ->
A + B + C.
-module(temp).
-export([f2c/1, c2f/1, convert/1]).
f2c(F) ->
5 * (F - 32) / 9.0.
c2f(C) ->
9 * C / 5.0 + 32.
convert({c, T}) ->
{f, c2f(T)};
convert({f, T}) ->
{c, f2c(T)}.
-module(time).
-export([swedish_date/0]).
integer_to_list_of_2(N) ->
if
N < 10 ->
"0" ++ integer_to_list(N);
true ->
integer_to_list(N)
end.
swedish_date() ->
{Y, M, D} = date(),
integer_to_list_of_2(Y rem 1000) ++
integer_to_list_of_2(M) ++
integer_to_list_of_2(D).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment