Hi, Marcello and Alexander. Perhaps we can move this to the mailing
list and off the JIRA ticket?

On Sun, Feb 5, 2012 at 11:21 PM, Marcello Nuccio (Commented) (JIRA)
<j...@apache.org> wrote:
>
>    [ 
> https://issues.apache.org/jira/browse/COUCHDB-1397?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13200798#comment-13200798
>  ]
>
> Marcello Nuccio commented on COUCHDB-1397:
> ------------------------------------------
>
> @Alexander,
> this isn't as off-topic as it seems at first, because software design is 
> heavily influenced by ease of testability.

Firstly, I'm unsure if you noticed but I made a typo which gave my
statement the opposite meaning.

I agree that emit(), log(), etc. should NOT be global.

However, I disagree that they should be passed to map(), simply
because it burdens the programmer. Plus, if we add new functions in
the future, it would change the call signature of map functions. On
the other hand, we get the same benefit by passing the functions but
using a closure to put them in scope of map(), reduce(), etc.

The map function might be built like so (the example is imperfect but
I'm aiming for clarity for now):

Given: ddoc.views.people.map = "module.exports = function(doc) {
emit(doc.name, 1) }"

var make_map = Function("require, module, exports, emit, log",
ddoc.views.people.map)
var map = make_map(require, module, exports, emit, log)

map({name:"Bob"}) // Runs emit("Bob", 1)

## Unit testing

It's imperfect for unit testing; however I think a hypothetical NPM
package would be fine. It could convert the ddoc functions to
JavaScript functions in a similar fashion. (Below is the first API
that popped into my head, no doubt we could make better.)

var assert = require('assert')
  , couch = require('mock-couchdb')

var ddoc = couch.load_ddoc({views: {people: {map: "function(doc) {
emit(doc.name, 1) }"}}})

var emitted
ddoc.on('emit', function(key, val) { emitted = key })
ddoc.views.people.map({name:"Bob"})
assert.equal(emitted, "Bob")

-- 
Iris Couch

Reply via email to