You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by John Beppu <jo...@gmail.com> on 2008/10/14 11:36:19 UTC

Not a big deal, but is the emit function really necessary?

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.

--beppu

Re: Not a big deal, but is the emit function really necessary?

Posted by Jan Lehnardt <ja...@apache.org>.
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
--