Skip to content

Instantly share code, notes, and snippets.

@wohali
Created February 24, 2014 06:39
Show Gist options
  • Save wohali/9182972 to your computer and use it in GitHub Desktop.
Save wohali/9182972 to your computer and use it in GitHub Desktop.
Simple Erlang views for CouchDB
%% this is an exact match for the original JS function
fun({Doc}) ->
case proplists:is_defined(<<"diet">>, Doc) of
true ->
Emit(proplists:get_value(<<"diet">>, Doc), 1);
false ->
ok
end
end.
%% This is also an exact function, but uses an inline
%% anonymous function just to show how additional functions
%% can be defined and included
fun({Doc}) ->
Foo = fun(D) ->
proplists:get_value(<<"diet">>, D)
end,
case proplists:is_defined(<<"diet">>, Doc) of
true ->
Emit(Foo(Doc), 1);
false ->
ok
end
end.
%% This version will emit (null, 1) for docs that do not define diet
%% as such it is not a faithful reproduction of the original view,
%% but it will not crash on docs missing diet.
fun({Doc}) ->
Emit(proplists:get_value(<<"diet">>, Doc, null), 1)
end.
// The simplest of map views in JavaScript. 90% of views look like this.
function(doc)
{
if(doc.diet)
{
emit(doc.diet, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment