You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by "Stock, Ingemar" <In...@Philotech.de> on 2013/01/07 13:12:06 UTC

Struggling with erlang

Hey all,

can somebody help me to translate this filter function into Erlang, pls?


	function(doc){
		if(doc.type === "tile"){
			return doc.key.length <= 8;
		}else{
			return true;
		}
	}


My first attempt was this, but I don't know how to do the "key.length <=
8" check and I'm not sure if this works at all:

	fun({Doc}) ->
		case {proplists:get_value(<<"type">>, Doc),
proplists:get_value(<<"key">>, Doc)} of
		{undefined, _} ->
			true;
		{_, undefined} ->
			true;
		{Type, Key} ->
			case {Type} of
			{<<"tile">>} ->
				true;
			_ ->
				false;
			end;
		_ ->
			true;
		end
	end.


Thank you,
Ingemar


AW: Struggling with erlang

Posted by "Stock, Ingemar" <In...@Philotech.de>.
Thanks!

It nearly works as you have written. I only had to add an additional argument in order to work as filter function:

fun({Props},{Req}) ->
	...



-----Ursprüngliche Nachricht-----
Von: Robert Newson [mailto:rnewson@apache.org] 
Gesendet: Montag, 7. Januar 2013 13:36
An: user@couchdb.apache.org
Betreff: Re: Struggling with erlang

Something like;

fun({Props}) ->
  couch_util:get_value(<<"type">>, Props) == <<"tile">> andalso
    couch_util:get_value(<<"key">>, Props) /= undefined andalso
    byte_size(couch_util:get_value(<<"key">>, Props)) =< 8
  end.

On 7 January 2013 12:12, Stock, Ingemar <In...@philotech.de> wrote:
>   function(doc){
>                 if(doc.type === "tile"){
>                         return doc.key.length <= 8;
>                 }else{
>                         return true;
>                 }
>         }

Re: Struggling with erlang

Posted by Robert Newson <rn...@apache.org>.
Something like;

fun({Props}) ->
  couch_util:get_value(<<"type">>, Props) == <<"tile">> andalso
    couch_util:get_value(<<"key">>, Props) /= undefined andalso
    byte_size(couch_util:get_value(<<"key">>, Props)) =< 8
  end.

On 7 January 2013 12:12, Stock, Ingemar <In...@philotech.de> wrote:
>   function(doc){
>                 if(doc.type === "tile"){
>                         return doc.key.length <= 8;
>                 }else{
>                         return true;
>                 }
>         }