Thank you, that works perfectly.

I guess the only way to understand how this works is for me to understand more 
about Erlang, if you have any pointers/tips for learning Erlang I would be very 
grateful (I come from a Perl, C, Java background)

Many thanks
Jeremy


________________________________
 From: david martin <[email protected]>
To: [email protected] 
Sent: Monday, 28 May 2012, 9:40
Subject: Re: Erlang reduce functions
 
try this
fun(Keys,Values,ReReduce)->
     case ReReduce of
         true->lists:sum(Values);
         false->length(Values)
     end
end.


On 27/05/12 16:33, Jeremy Smith wrote:
> Hi,
>
> I am new to erlang, just started today, because we were advised to move some 
> of our views to native erlang views, I am trying to write a reduce function:
>
> The Values its using seem to be in the form:
> [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
> 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
>
>
> so I have tried to do the following to get a count of all the 1's, but it 
> only gives me the count of the outermost lists (in the above case 3, instead 
> of 60(ish))
>
> My Reduce funtion:
> fun(Keys, Values, ReReduce) ->  lists:flatlength(Values) end.
>
>
> My Map function looks like this:
> fun({Doc}) ->
>      Emitter = fun(Doc) ->
>          Date           = proplists:get_value(<<"publish_date">>,   Doc),
>
>          Year = string:substr(erlang:bitstring_to_list(Date), 1, 4),
>          Emit(erlang:list_to_bitstring(Year), 1)
>      end,
>
>      HasRequiredFields = fun(Doc) ->
>          case {proplists:is_defined(<<"a_number">>, Doc), 
>proplists:is_defined(<<"publish">>, Doc), 
>proplists:is_defined(<<"publish_date">>, Doc)} of
>              {true, true, true} ->
>                  Emitter(Doc);
>
>              _->
>                  false
>          end
>      end,
>
>      HasRequiredFields(Doc)
> end.
>
> Cheers
> Jeremy

Reply via email to