Skip to content

Instantly share code, notes, and snippets.

@jimmybot
Created August 18, 2015 19:37
Show Gist options
  • Save jimmybot/1c6b5b98c39d04d58eb9 to your computer and use it in GitHub Desktop.
Save jimmybot/1c6b5b98c39d04d58eb9 to your computer and use it in GitHub Desktop.
-module(gameoflife).
-export([ping/2, start/0, clock/0]).
% currying in erlang?
clock() ->
Cells = [ul, ur, ll, lr],
lists:map(fun(Cell) -> timer:send_after(1000, Cell, update) end, Cells),
clock_receive(length(Cells)).
%clock().
clock_receive(0) ->
ok;
clock_receive(N) ->
receive
{update, Cell, State} ->
io:format("received ~p ~p~n", [Cell, State]),
put(Cell, State)
end,
clock_receive(N - 1).
% implement message passing between cells to get state
% scopes in erlang?
ping(A, Cells) ->
ping(A, 0, Cells).
ping(A, State, Cells) ->
io:format("Hello, I am ~p~n", [A]),
receive
update ->
{_, _, C} = time(),
io:format("Update time ~p~n", [C]),
Other_Cells = lists:filter(fun(X) X =/= A end, Cells),
clock_master ! {update, A, State},
get_state -> io:format("Get state called~n");
B -> io:format("This is message ~s~n", [B])
end,
io:format("Hello2, I am ~s~n", [A]),
ping(A, State, Cells).
% convert atoms to variables that represent cartesian grid
start() ->
Cells = [ul, ur, ll, lr],
F = fun(X) -> register(X, spawn(gameoflife, ping, [X, Cells])) end,
lists:map(F, Cells),
register(clock_master, spawn(gameoflife, clock, [])).
% can you send messages to top level (unspawned process)?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment