You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Troy Kruthoff <tk...@gmail.com> on 2010/02/11 14:33:56 UTC

My first native erlang views

I went google diving and learned just enough Erlang to write a couple  
view functions (that work), but I'm hoping someone with more Erlang  
experience can offer up some suggestions for improvement.  Plus, I'd  
like to get some samples of erl views on the wiki so the next guys  
have it a little easier.

fun({Doc}) ->
   case proplists:get_value(<<"json_class">>, Doc, null) ==  
<<"Account">> of
   	true  -> Emit(proplists:get_value(<<"screen_name">>, Doc,  
null),null) ;
   	false -> ok
   end
end.


fun({Doc}) ->
   %% how does one do the equiv js if (doc.json_class! 
="WhateverClass") return(null);
   %% that would sure beat wrapping all this code in a silly case,  
like the view above!
   Categories = proplists:get_value(<<"categories">>,Doc,""),
   Tokenize = fun(WordStr,Prefix) when is_bitstring(WordStr) ->
     Tokens = string:tokens(bitstring_to_list(WordStr),",| "),
     lists:foreach(fun(Token) -> Emit(list_to_bitstring(Prefix++Token), 
1) end, Tokens)
   end,

   lists:foreach(
     fun(Category) ->
       CategoryDoc = element(1,Category),
       catch  
Tokenize(proplists:get_value(<<"words">>,CategoryDoc,null),"wrd:"),
       catch  
Tokenize(proplists:get_value(<<"follow">>,CategoryDoc,null),"usr:")
     end, Categories)
end.

%% Reduce Function (from the wiki, are there builtin's for sum like in  
js (tried "sum" -- nogo) ?)
fun(Keys, Values, ReReduce) -> length(Values) end.