incubator-couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Lehnardt <...@apache.org>
Subject Re: Not a big deal, but is the emit function really necessary?
Date Tue, 14 Oct 2008 10:02:53 GMT

On Oct 14, 2008, at 11:36 , John Beppu wrote:

> While working on a map function, it occurred to me that instead of  
> emit(key,
> doc) what if I could just return [ [key, doc], ... ].
>
> Again, not a big deal.  I just thought it would be more concise.

I think it makes for cleaner code when there are explicit calls to  
`emit()`
instead of having to build up a magic data structure that later can get
returned.

function(doc) {
   if(!doc.stuff) { return; }
   for(var idx in doc.stuff) {
     emit(doc.stuff, 1);
   }
}

vs.

function(doc) {
   if(!doc.stuff) { return; }
   var return_val = []
   for(var idx in doc.stuff) {
     return_val.push([doc.stuff[idx], 1]);
   }
   return return_val;
}

`emit()` is nothing but an alias to Array.push() internally:

emit = function(key, value) {
   map_results.push([key, value]);
}

(from share/server/main.js)

Cheers
Jan
--

Mime
View raw message