On 06/02/2011 01:49 PM, Sylvain Niles wrote:
Is there any open source code out there using erlang functions via
ripple or rest that I can look at to see a fully functional flow?
I made a lot of stupid mistakes getting this to work. Leaving add_paths
commented out, not using arrays in arguments to #map and #reduce, not
exporting the functions, incorrect arities, not compiling the code...
etc. Hopefully this helps some.
Oh, and forgive the erlang; this was my first function. ;-)
--Kyle
-module(structs).
-export([
from_dict/1,
sort_by_value/2,
unique/2
]).
...
% Sorts a list of structs by a given value.
sort_by_value(Structs, [Key, <<"asc">>]) ->
lists:sort(
fun({struct, A}, {struct, B}) ->
proplists:get_value(Key, A) < proplists:get_value(Key, B)
end,
Structs
);
sort_by_value(Structs, [Key, <<"desc">>]) ->
lists:sort(
fun({struct, A}, {struct, B}) ->
proplists:get_value(Key, A) > proplists:get_value(Key, B)
end,
Structs
);
sort_by_value(Structs, Key) ->
sort_by_value(Structs, [Key, <<"asc">>])
--------------------------------------------------------------------
{riak_kv, [
...
{add_paths, ["/etc/riak/erl/"]},
...
]}
--------------------------------------------------------------------
$ cd /etc/riak/erl/
$ erlc structs.erl
$ riak attach
> l(structs).
--------------------------------------------------------------------
results = Tablet::Comment.mr(items).
map(['tablet', 'map_to_links_with_data'], :arg => 'user').
map(['tablet', 'map_to_keydata_with_data'], :arg => ['user',
['name', 'thumbnail']]).
reduce(['structs', 'sort_by_value'], :arg => ['created_at',
'desc'], :keep => true).run
--Kyle
_______________________________________________
riak-users mailing list
[email protected]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com