incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Troy Kruthoff <tkruth...@gmail.com>
Subject My first native erlang views
Date Thu, 11 Feb 2010 13:33:56 GMT
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.


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message