1. Fred T-H
  2. Learn You Some Erlang

Commits

Fred T-H  committed 862bc17

small changes and adding QLC stuff

  • Participants
  • Parent commits 7d4f193
  • Branches default

Comments (0)

Files changed (2)

File mafiapp-1.0.0/src/mafiapp.erl

View file
 %%% PRIVATE %%%
 %%%%%%%%%%%%%%%
 
-%% This could be more efficient as two queries with indexes, but will
-%% be reworked with QLC
 find_services(Name) ->
     Match = ets:fun2ms(
             fun(#mafiapp_services{from=From, to=To, date=D, description=Desc})

File mafiapp-1.0.1/src/mafiapp.erl

View file
 
 debts(Name) ->
     F = fun() ->
-        qlc:eval(qlc:q(
+        QH = qlc:q(
             [if Name =:= To -> {From,1};
                 Name =:= From -> {To,-1}
              end || #mafiapp_services{from=From, to=To} <-
                       mnesia:table(mafiapp_services),
-                   Name =:= To orelse Name =:= From]))
+                   Name =:= To orelse Name =:= From]),
+        qlc:fold(fun({Person,N}, Dict) ->
+                  dict:update(Person, fun(X) -> X + N end, N, Dict)
+                 end,
+                 dict:new(),
+                 QH)
     end,
-    Dict = lists:foldl(fun({Person,N}, Dict) ->
-                        dict:update(Person, fun(X) -> X + N end, N, Dict)
-                       end,
-                       dict:new(),
-                       mnesia:activity(transaction, F)),
-    lists:sort([{V,K} || {K,V} <- dict:to_list(Dict)]).
+    lists:sort([{V,K} || {K,V} <- dict:to_list(mnesia:activity(transaction, F))]).
 
 add_enemy(Name, Info) ->
     F = fun() -> mnesia:write(#mafiapp_enemies{name=Name, info=Info}) end,
 %%% PRIVATE %%%
 %%%%%%%%%%%%%%%
 
-%% This could be more efficient as two queries with indexes, but will
-%% be reworked with QLC
 find_services(Name) ->
     Match = ets:fun2ms(
             fun(#mafiapp_services{from=From, to=To, date=D, description=Desc})