From 79a2bc14dee4d9c393bb8046375f28b0ab177b3c Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Sat, 28 Jul 2012 14:29:16 -0400 Subject: [PATCH] Organized reused functions into lib module. --- 001/src/life_cell.erl | 18 ++---------------- 001/src/life_lib.erl | 20 ++++++++++++++++++++ 001/src/life_time.erl | 8 +------- 3 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 001/src/life_lib.erl diff --git a/001/src/life_cell.erl b/001/src/life_cell.erl index 9f7b171..4ee07ac 100644 --- a/001/src/life_cell.erl +++ b/001/src/life_cell.erl @@ -73,11 +73,11 @@ handle_cast({next_gen, GenID}, ,early_msgs=EarlyMsgs }=State) -> - ok = cast_one2all(Neighbors, {state_broadcast, GenID, CellState}), + ok = life_lib:cast_one2all(Neighbors, {state_broadcast, GenID, CellState}), % Put stashed messages back in the mailbox, % now that we're ready to handle them - ok = cast_all2one(Name, EarlyMsgs), + ok = life_lib:cast_all2one(Name, EarlyMsgs), NewState = State#state{replies_pending=NumNeighbors ,gen_id=GenID @@ -141,20 +141,6 @@ handle_cast(_Msg, State) -> %% Internal %% ============================================================================ -% Cast all messages to one destination -cast_all2one(_, []) -> ok; -cast_all2one(Server, [Msg | Msgs]) -> - ok = gen_server:cast(Server, Msg), - cast_all2one(Server, Msgs). - - -% Cast one message to all destinations -cast_one2all([], _) -> ok; -cast_one2all([Server | Servers], Msg) -> - ok = gen_server:cast(Server, Msg), - cast_one2all(Servers, Msg). - - new_state(1, LiveNeighbors) when LiveNeighbors < 2 -> 0; new_state(1, LiveNeighbors) when LiveNeighbors < 4 -> 1; new_state(1, LiveNeighbors) when LiveNeighbors > 3 -> 0; diff --git a/001/src/life_lib.erl b/001/src/life_lib.erl new file mode 100644 index 0000000..3b26b08 --- /dev/null +++ b/001/src/life_lib.erl @@ -0,0 +1,20 @@ +-module(life_lib). + +-export([cast_one2all/2 + ,cast_all2one/2 + ]). + + +% Cast all messages to one destination +cast_all2one(_, []) -> ok; +cast_all2one(Server, [Msg | Msgs]) -> + ok = gen_server:cast(Server, Msg), + cast_all2one(Server, Msgs). + + +% Cast one message to all destinations +cast_one2all([], _) -> ok; +cast_one2all([Server | Servers], Msg) -> + ok = gen_server:cast(Server, Msg), + cast_one2all(Servers, Msg). + diff --git a/001/src/life_time.erl b/001/src/life_time.erl index 22d3300..c3b7a45 100644 --- a/001/src/life_time.erl +++ b/001/src/life_time.erl @@ -86,7 +86,7 @@ handle_cast(next_gen, }=State) -> NewGenID = GenID + 1, - ok = cast_all(Cells, {next_gen, NewGenID}), + ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}), NewState = State#state{replies_pending=NumCells ,gen_id=NewGenID ,num_dead=0 @@ -151,12 +151,6 @@ schedule_next_gen() -> ok = gen_server:cast(?MODULE, next_gen). -cast_all([], _) -> ok; -cast_all([Server | Servers], Msg) -> - ok = gen_server:cast(Server, Msg), - cast_all(Servers, Msg). - - state_to_char(0) -> ?CHAR_DEAD; state_to_char(1) -> ?CHAR_ALIVE. -- 2.20.1