You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Christoph Bonitz <cb...@gmail.com> on 2008/03/04 10:10:45 UTC

Calling other functions in views

Hello everybody,

I am trying to create several views that differ only in the value of
an integer variable in the javascript-function. I would love to do
something like

function(doc,n) {/* do something that depends on n*/}

but the view functions take only one argument. Currently I am
replicating code, i.e.

function(doc) {n = 1; /*snip*/}
function(doc) {n = 2; /*snip*/}
etc.

Is there a way to call another javascript function from a view function, i.e.

function(doc) {otherfunction(doc,1);}

or pass a parameter from the http-query? I have found nothing like
this in the wiki or on the web.

If there is a solution, I will create a wiki-page.

Thanks and best regards
Christoph

Re: Calling other functions in views

Posted by Christoph Bonitz <cb...@gmail.com>.
Dear Christian,

thank you for your reply.

2008/3/4, Christopher Lenz <cm...@gmx.de>:
> What exactly are you trying to do here? This sounds a bit like you
>  haven't discovered key/startkey/endkey filtering yet. Or that that a
>  single view function can return multiple rows.

I am computing word n-grams for different n.
http://en.wikipedia.org/wiki/N-gram

I used the n-gram as a key and the document as a value. Thanks for
your remark about key filtering, it got me to think of simply using an
array as a key, which contains the n-gram as well as the n.

Best regards and thanks a lot
Christoph

Re: Calling other functions in views

Posted by Christopher Lenz <cm...@gmx.de>.
On 04.03.2008, at 10:10, Christoph Bonitz wrote:
> I am trying to create several views that differ only in the value of
> an integer variable in the javascript-function. I would love to do
> something like
>
> function(doc,n) {/* do something that depends on n*/}
>
> but the view functions take only one argument. Currently I am
> replicating code, i.e.
>
> function(doc) {n = 1; /*snip*/}
> function(doc) {n = 2; /*snip*/}
> etc.

What exactly are you trying to do here? This sounds a bit like you  
haven't discovered key/startkey/endkey filtering yet. Or that that a  
single view function can return multiple rows.

> Is there a way to call another javascript function from a view  
> function, i.e.
>
> function(doc) {otherfunction(doc,1);}

No, you currently don't have access to any shared code outside of the  
Javascript standard objects.

I think it might be nice if one could provide shared code via design  
documents, but how exactly that would work (considering also the  
support for different view languages/servers) would need to be fleshed  
out.

> or pass a parameter from the http-query? I have found nothing like
> this in the wiki or on the web.

One important rule for view functions is that any data that is not  
contained in the currently processed document (or that can be  
deterministically derived from that document) must not be used in  
views, even if it was technically possible (`new Date()` for getting  
the current date/time, for example).

That is because the view function is run only on new/changed  
documents, not on all docs every time the view is queried.

Cheers,
Chris
--
Christopher Lenz
   cmlenz at gmx.de
   http://www.cmlenz.net/