[erlang-questions] newbie question

Andreas Hillqvist andreas.hillqvist@REDACTED
Thu Nov 29 10:53:20 CET 2007


Like Andreas(the other Andreas ;-) ) proposed fold fits the purpose.

Here is an complete example:

-module(aggregate).
-export([start/0]).
-record(item, {count, item, user}).
-include_lib("stdlib/include/qlc.hrl").

start() ->
    mnesia:start(),
    mnesia:create_schema([]),

    TabDef = [{ram_copies, [node()]},
              {index, [item]},
              {attributes, record_info(fields, item)}],
    mnesia:create_table(item, TabDef),

    {atomic,ok} = mnesia:transaction(fun insert_query/0),

    mnesia:transaction(fun select_query/0).

insert_query() ->
    Items = [#item{count = 4,
                   item  = apple,
                   user  = john},
             #item{count = 7,
                   item  = apple,
                   user  = mary},
             #item{count = 3,
                   item  = oranges,
                   user  = jane},
             #item{count = 2,
                   item  = oranges,
                   user  = john},
             #item{count = 2,
                   item  = bananas,
                   user  = mary},
             #item{count = 5,
                   item  = bananas,
                   user  = jane},
             #item{count = 2,
                   item  = bananas,
                   user  = john}],

    lists:foreach(fun mnesia:write/1, Items).

select_query() ->
    Q = qlc:keysort(#item.item, mnesia:table(item)),
    qlc:fold(fun aggregate/2, [], Q).

aggregate(#item{count = Count, item = Item}, [{Item, Sum} | Acc]) ->
    [{Item, Count + Sum} | Acc];
aggregate(#item{count = Count, item = Item}, Acc) ->
    [{Item, Count} | Acc].


I am uncertain if the index for item, gives keysort any performance gain.
Maybe some one else can answer this?

As always I appreciate any comments and critic. :-D


Regards
Andreas Hillqvist

2007/11/28, Ukyo Virgden <listproc@REDACTED>:
> Hi,
>
> I've just jumped into Erlang. Almost finished reading Joe Armstrong's
> book. While testing with mnesia, I've come across a problem I hope
> you guys can shed a light.
>
> Consider I have records like (count, item, user)
>
> {4, apple, john}
> {7, apple, mary}
> {3, oranges, jane}
> {2, oranges, john}
> {2, bananas, mary}
> {5, bananas, jane}
> {2, bananas, john}
>
> I can store in mnesia and retrieve with qlc. But I could not "group"
> them. I figured out how inappropriate it'd be to ask is qlc has a
> facility like SQL GROUP BY. :)
>
> Can anybody help me how can I group returned list? Or in other words,
> how can I apply an aggregate function to get sum of items, count of
> items etc.? I've already figured out I can send my process some
> messages and collect data but I believe that'd be slower than being
> able to write a recursive function.
>
> I'd appreciate if you could provide some pointers to resources for
> functional programming techniques in Erlang.
>
> Thanks in advance
> Ukyo Virgden
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list