I would imagine that it's not very easy if you want to retain the ability to instantiate objects outside the function.
But I'm no expert :) Ben On Tue, Nov 18, 2008 at 2:59 PM, Paul Davis <[EMAIL PROTECTED]> wrote: > If this is easy and you see an implementation, I think it'd be a great > addition to force the sand boxing of individual functions. > > That said, I don't know much about the JS internals so I'm not sure > how hard it would be to auto detect what might cause side effects > outside the function and what wouldn't. I've stuck largish objects > outside the function itself on the assumption that they'll only be > instantiated once as opposed to once per document. > > I have absolutely no idea if that's even necessary though. > > Paul > > On Tue, Nov 18, 2008 at 9:51 AM, Benjamin Nortier <[EMAIL PROTECTED]> wrote: >> Should CouchDB not restrict defining state outside the map function? >> I.e. you should not be able to declare a counter outside the function? >> >> On Tue, Nov 18, 2008 at 1:42 PM, Jedediah Smith >> <[EMAIL PROTECTED]> wrote: >>> >>> You can't maintain a state across calls to map functions in this way. Map >>> functions can be called in any order or in parallel, any number of times on >>> a particular document and in completely separate environments. They should >>> not have any side effects or depend on any outside state. >>> >>> You should read up on Google's MapReduce and maybe functional programming in >>> general in order to understand how CouchDB works. >>> >>> If you just want to number the results of your view, that can be easily done >>> by the calling code. >>> >>> Adam Groves wrote: >>>> >>>> Hi, >>>> >>>> I've got the following view: >>>> >>>> count = 0; >>>> function(doc) { >>>> if(doc.type == "document") { >>>> count = count + 1 >>>> emit(doc._id, count); >>>> } >>>> } >>>> >>>> The idea is that I get a list of documents, each having a counter >>>> value which is incremental. I expected the count values to come out in >>>> order, but that isn't the case: my first few documents have values of >>>> 62, 61, 22, 19. I'm not quite sure what's going on here - any ideas >>>> how the order is being set here? >>>> >>>> Regards >>>> >>>> Adam Groves >>>> >>> >> >> >> >> -- >> Benjamin Nortier >> e: [EMAIL PROTECTED] >> c: +44 (0)778 946 1959 >> msn: [EMAIL PROTECTED] >> gtalk: [EMAIL PROTECTED] >> > -- Benjamin Nortier e: [EMAIL PROTECTED] c: +44 (0)778 946 1959 msn: [EMAIL PROTECTED] gtalk: [EMAIL PROTECTED]