Created
February 24, 2014 06:39
-
-
Save wohali/9182972 to your computer and use it in GitHub Desktop.
Simple Erlang views for CouchDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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