Anton Shestakov <engored@ya.ru>, Mon, 03 Mar 2014 20:00:26 +0900
Move parse_rules and winning_hand to rps_rules.
src/rps_rules.erl
Permissions: -rw-r--r--
-export([parse/1, winning_hand/2]). -record(state, {hands=[], beats=dict:new(), loses=dict:new(), why=dict:new()}). Tokens = lists:map(fun list_to_atom/1, string:tokens(Text, " ")), parse([V1,Label,V2|T], #state{hands=Hands, beats=Beats, loses=Loses, why=Why}) -> beats=dict:append(V1, V2, Beats), loses=dict:append(V2, V1, Loses), why=dict:store({V1, V2}, Label, Why)}, % rock, rock, rock -> rock % rock, paper, scissors -> undefined % rock, paper, paper -> paper % rock, rock, paper -> paper winning_hand(Hands, State=#state{}) -> HSet = sets:from_list(Hands), Loses = dict:fetch(Hand, State#state.loses), case sets:is_disjoint(sets:from_list(Loses), HSet) of