You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Rangachari Anand <rx...@gmail.com> on 2009/04/22 15:55:11 UTC

The sum function (used in reduce)

I searched all over the wiki and did not see any description of the
sum function that can be called in the reduce method of
a view. I can guess what it does but a pointer to more
concrete information would be appreciated.

There is only a passing reference to it in the page
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views

Are there any other handy functions that could be called from inside
map or reduce?

Thanks
Rangachari Anand

Re: The sum function (used in reduce)

Posted by Jan Lehnardt <ja...@apache.org>.
On 22 Apr 2009, at 07:25, Zachary Zolton wrote:

> I've also expressed interest in both:
>
> 1) Adding/devising more helper functions
>
> 2) Upgrading SpiderMonkey to JavaScript version 1.8

We're linking against whatever lib[moz]js can be found in the
library paths. If there are no incompatible API changes,
1.8 should "just work". If there's work required, we can
start putting it in place for the forthcoming stable release
of 1.8.

Cheers
Jan
--


>
> This would add a LOT features useful for succinct JavaScript Map/ 
> reduce code:
>  * More functional methods on Array objects, such as reduce()
>  * Destructuring assignments
>  * Short-cut Lambda notation (ooh la la!)
>
> Of course, SpiderMonkey 1.8 is still in RC1, however anyone who's been
> using Firefox 3 has been exposed to it!
>
> On Wed, Apr 22, 2009 at 9:00 AM, Thomas Heller <th...@gmail.com>  
> wrote:
>> Hey,
>>
>> you can find it in COUCH_HOME/share/server/main.js
>>
>> sum = function(values) {
>>  var rv = 0;
>>  for (var i in values) {
>>    rv += values[i];
>>  }
>>  return rv;
>> }
>>
>> And no, its the only "helper" function.
>>
>> Regards,
>> /thomas
>>
>>
>> On Wed, Apr 22, 2009 at 3:55 PM, Rangachari Anand  
>> <rx...@gmail.com> wrote:
>>
>>> I searched all over the wiki and did not see any description of the
>>> sum function that can be called in the reduce method of
>>> a view. I can guess what it does but a pointer to more
>>> concrete information would be appreciated.
>>>
>>> There is only a passing reference to it in the page
>>> http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
>>>
>>> Are there any other handy functions that could be called from inside
>>> map or reduce?
>>>
>>> Thanks
>>> Rangachari Anand
>>>
>>
>


Re: The sum function (used in reduce)

Posted by Chris Anderson <jc...@apache.org>.
On Sat, Apr 25, 2009 at 10:25 AM, Chris Anderson <jc...@apache.org> wrote:
> On Thu, Apr 23, 2009 at 12:19 PM, Zachary Zolton
> <za...@gmail.com> wrote:
>> CouchApp == Not necessarily much help to the rest of us.
>>
>
> I'm curious what you mean by this. It should be the most basic way to
> maintain design documents. I guess if you are generating your views
> from your application code it might not fit well.
>

Doh. Loading the whole thread fail. This was already answered. I guess
it's worth repeating: CouchApp is merely a tool to let you manage
design documents in your filesystem.

> --
> Chris Anderson
> http://jchrisa.net
> http://couch.io
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Re: The sum function (used in reduce)

Posted by Zachary Zolton <za...@gmail.com>.
You guessed it. I'm using CouchRest to generate certain views, however  
other views I need to create manually.

As I've already stated, I'm willing to give it a try. So, we'll see  
how it goes.

On Apr 25, 2009, at 7:25 AM, Chris Anderson <jc...@apache.org> wrote:

> On Thu, Apr 23, 2009 at 12:19 PM, Zachary Zolton
> <za...@gmail.com> wrote:
>> CouchApp == Not necessarily much help to the rest of us.
>>
>
> I'm curious what you mean by this. It should be the most basic way to
> maintain design documents. I guess if you are generating your views
> from your application code it might not fit well.
>
> -- 
> Chris Anderson
> http://jchrisa.net
> http://couch.io

Re: The sum function (used in reduce)

Posted by Chris Anderson <jc...@apache.org>.
On Thu, Apr 23, 2009 at 12:19 PM, Zachary Zolton
<za...@gmail.com> wrote:
> CouchApp == Not necessarily much help to the rest of us.
>

I'm curious what you mean by this. It should be the most basic way to
maintain design documents. I guess if you are generating your views
from your application code it might not fit well.

-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Re: The sum function (used in reduce)

Posted by Jan Lehnardt <ja...@apache.org>.
On 24 Apr 2009, at 11:26, Zachary Zolton wrote:

> Interesting. I guess I thought CouchApp was just for pure-AJAX apps...

yeah, the branding is confusing, we're thinking about making this more  
clear.

Cheers
Jan
--


>
> After a second thought, I'm gonna give it a whirl. Perhaps I can  
> find a way for it to cohabitate with my CouchRest design docs.
>
> On Apr 24, 2009, at 12:53 AM, Nils Breunese <n....@vpro.nl>  
> wrote:
>
>> Jan Lehnardt wrote:
>>
>>> On 23 Apr 2009, at 09:19, Zachary Zolton wrote:
>>>> CouchApp == Not necessarily much help to the rest of us.
>>> Why not? It's just pushing a directory tree to a design doc. The  
>>> fact
>>> that you can use that to do standalone apps doesn't stop you from
>>> using it for your non-standalone app :)
>>
>> Having your CouchDB design doc as a tree of normal files is also  
>> very nice because you can put that into a version control system. I  
>> definitely recommend using couchapp.
>>
>> Nils Breunese.
>


Re: The sum function (used in reduce)

Posted by Zachary Zolton <za...@gmail.com>.
Interesting. I guess I thought CouchApp was just for pure-AJAX apps...

After a second thought, I'm gonna give it a whirl. Perhaps I can find  
a way for it to cohabitate with my CouchRest design docs.

On Apr 24, 2009, at 12:53 AM, Nils Breunese <n....@vpro.nl> wrote:

> Jan Lehnardt wrote:
>
>> On 23 Apr 2009, at 09:19, Zachary Zolton wrote:
>>> CouchApp == Not necessarily much help to the rest of us.
>> Why not? It's just pushing a directory tree to a design doc. The fact
>> that you can use that to do standalone apps doesn't stop you from
>> using it for your non-standalone app :)
>
> Having your CouchDB design doc as a tree of normal files is also  
> very nice because you can put that into a version control system. I  
> definitely recommend using couchapp.
>
> Nils Breunese.

Re: The sum function (used in reduce)

Posted by Nils Breunese <n....@vpro.nl>.
Jan Lehnardt wrote:

> On 23 Apr 2009, at 09:19, Zachary Zolton wrote:
> 
>> CouchApp == Not necessarily much help to the rest of us.
> 
> Why not? It's just pushing a directory tree to a design doc. The fact
> that you can use that to do standalone apps doesn't stop you from
> using it for your non-standalone app :)

Having your CouchDB design doc as a tree of normal files is also very 
nice because you can put that into a version control system. I 
definitely recommend using couchapp.

Nils Breunese.

Re: The sum function (used in reduce)

Posted by Jan Lehnardt <ja...@apache.org>.
On 23 Apr 2009, at 09:19, Zachary Zolton wrote:

> CouchApp == Not necessarily much help to the rest of us.

Why not? It's just pushing a directory tree to a design doc. The fact
that you can use that to do standalone apps doesn't stop you from
using it for your non-standalone app :)

Cheers
Jan
--

>
> Seriously, though, I've heard this before and decided instead to
> handle this in my application code...
>
> On Thu, Apr 23, 2009 at 10:06 AM, Nils Breunese <n....@vpro.nl>  
> wrote:
>> Nils Breunese wrote:
>>
>>> Adam Wolff wrote:
>>>
>>>> It'd be great to make it so that _design docs can define helper
>>>> functions too, so that two views can share a helper.
>>>
>>> You can already define functions in a separate file and use macros  
>>> to
>>>  'import' them when defining view functions.
>>
>> For clarity: macros are a couchapp feature. Examples of the use of ! 
>> json and
>> !code macros can be found in Sofa [0], for instance in lists/ 
>> index.js. They
>> look like comments (starting with '//' and all), but those lines are
>> processed by couchapp when pushing an app to CouchDB.
>>
>> Nils Breunese.
>>
>> [0] http://github.com/jchris/sofa/tree/master
>>
>


Re: The sum function (used in reduce)

Posted by Zachary Zolton <za...@gmail.com>.
CouchApp == Not necessarily much help to the rest of us.

Seriously, though, I've heard this before and decided instead to
handle this in my application code...

On Thu, Apr 23, 2009 at 10:06 AM, Nils Breunese <n....@vpro.nl> wrote:
> Nils Breunese wrote:
>
>> Adam Wolff wrote:
>>
>>> It'd be great to make it so that _design docs can define helper
>>> functions too, so that two views can share a helper.
>>
>> You can already define functions in a separate file and use macros to
>>  'import' them when defining view functions.
>
> For clarity: macros are a couchapp feature. Examples of the use of !json and
> !code macros can be found in Sofa [0], for instance in lists/index.js. They
> look like comments (starting with '//' and all), but those lines are
> processed by couchapp when pushing an app to CouchDB.
>
> Nils Breunese.
>
> [0] http://github.com/jchris/sofa/tree/master
>

Re: The sum function (used in reduce)

Posted by Nils Breunese <n....@vpro.nl>.
Nils Breunese wrote:

> Adam Wolff wrote:
> 
>> It'd be great to make it so that _design docs can define helper
>> functions too, so that two views can share a helper.
> 
> You can already define functions in a separate file and use macros to  
> 'import' them when defining view functions.

For clarity: macros are a couchapp feature. Examples of the use of !json 
and !code macros can be found in Sofa [0], for instance in 
lists/index.js. They look like comments (starting with '//' and all), 
but those lines are processed by couchapp when pushing an app to CouchDB.

Nils Breunese.

[0] http://github.com/jchris/sofa/tree/master

Re: The sum function (used in reduce)

Posted by Wojciech Kaczmarek <ka...@gmail.com>.
On Sun, Apr 26, 2009 at 02:41, Paul Davis <pa...@gmail.com> wrote:
> Wojciech Kaczmarek wrote:
>>
>> 2009/4/25 Paul Davis <pa...@gmail.com>:
>>
>>>
>>> On Sat, Apr 25, 2009 at 2:58 PM, Wojciech Kaczmarek
>>> <ka...@gmail.com> wrote:
>>>
>>>>
>>>> So the result of CouchApp push are just injected code chunks, right?
>>>> What about code size for really complicated macro sets, is it
>>>> irrelevant in practice?
>>>>
>>>
>>> Yeah, the code is just injected inline. I wouldn't worry about code
>>> size unless you're doing something fairly strange. For normal use
>>> which includes things like pulling in entire templating systems I've
>>> never heard of an issue.
>>>
>>
>> And what about handmade view servers in other languages? Is function
>> hashing/storing done by couchdb itself and viewserver:add_fun receives
>> the function text to execute, but doesn't care about storing?
>>
>
> Yup. The view server doesn't care about the signatures. The logic for when
> to reindex what is all handled in Erlang.

This is simple & cool. I think I owe the one who invented this
architecture his favorite (alcoholic) beverage.. ;]

Re: The sum function (used in reduce)

Posted by Paul Davis <pa...@gmail.com>.
Wojciech Kaczmarek wrote:
> 2009/4/25 Paul Davis <pa...@gmail.com>:
>   
>> On Sat, Apr 25, 2009 at 2:58 PM, Wojciech Kaczmarek
>> <ka...@gmail.com> wrote:
>>     
>>> So the result of CouchApp push are just injected code chunks, right?
>>> What about code size for really complicated macro sets, is it
>>> irrelevant in practice?
>>>       
>> Yeah, the code is just injected inline. I wouldn't worry about code
>> size unless you're doing something fairly strange. For normal use
>> which includes things like pulling in entire templating systems I've
>> never heard of an issue.
>>     
>
> And what about handmade view servers in other languages? Is function
> hashing/storing done by couchdb itself and viewserver:add_fun receives
> the function text to execute, but doesn't care about storing?
>   
Yup. The view server doesn't care about the signatures. The logic for 
when to reindex what is all handled in Erlang.

HTH,
Paul Davis

Re: The sum function (used in reduce)

Posted by Wojciech Kaczmarek <ka...@gmail.com>.
2009/4/25 Paul Davis <pa...@gmail.com>:
> On Sat, Apr 25, 2009 at 2:58 PM, Wojciech Kaczmarek
> <ka...@gmail.com> wrote:
>> So the result of CouchApp push are just injected code chunks, right?
>> What about code size for really complicated macro sets, is it
>> irrelevant in practice?
>
> Yeah, the code is just injected inline. I wouldn't worry about code
> size unless you're doing something fairly strange. For normal use
> which includes things like pulling in entire templating systems I've
> never heard of an issue.

And what about handmade view servers in other languages? Is function
hashing/storing done by couchdb itself and viewserver:add_fun receives
the function text to execute, but doesn't care about storing?

Re: The sum function (used in reduce)

Posted by Paul Davis <pa...@gmail.com>.
On Sat, Apr 25, 2009 at 2:58 PM, Wojciech Kaczmarek
<ka...@gmail.com> wrote:
> On Sat, Apr 25, 2009 at 20:15, Paul Davis <pa...@gmail.com> wrote:
>> On Sat, Apr 25, 2009 at 2:04 PM, Chris Anderson <jc...@apache.org> wrote:
>>> On Sat, Apr 25, 2009 at 11:05 AM, Wojciech Kaczmarek
>>> <ka...@gmail.com> wrote:
>>>> Now I see similar questions emerged. So my point is now: Is there a
>>>> way, existing or planned, to share some functions between views
>>>> without using couchapp macros?
>>>
>>> No such plans. The functions are stored according to a hash of their
>>> byte representation, so it's important that the function doesn't load
>>> any additional code, changing its behavior without changing its
>>> byte-string. Hence the use-case for something like CouchApp.
>>
>> The reason for the hashing is that we need to know when the function
>> changes to know when to reindex documents. Storing library code
>> somewhere ends up making this a bit more complicated. I wouldn't call
>> it out of the question to have something, but I don't think its on
>> anyone's agenda as things like CouchApp alleviate most of the need for
>> it.
>
> I see. This is quite clever hack. I like its simplicity (Couchdb
> shines this way in other aspects as well, so kudos to all implementors
> :-).
> So the result of CouchApp push are just injected code chunks, right?
> What about code size for really complicated macro sets, is it
> irrelevant in practice?
>

Yeah, the code is just injected inline. I wouldn't worry about code
size unless you're doing something fairly strange. For normal use
which includes things like pulling in entire templating systems I've
never heard of an issue.

HTH,
Paul Davis

Re: The sum function (used in reduce)

Posted by Wojciech Kaczmarek <ka...@gmail.com>.
On Sat, Apr 25, 2009 at 20:15, Paul Davis <pa...@gmail.com> wrote:
> On Sat, Apr 25, 2009 at 2:04 PM, Chris Anderson <jc...@apache.org> wrote:
>> On Sat, Apr 25, 2009 at 11:05 AM, Wojciech Kaczmarek
>> <ka...@gmail.com> wrote:
>>> Now I see similar questions emerged. So my point is now: Is there a
>>> way, existing or planned, to share some functions between views
>>> without using couchapp macros?
>>
>> No such plans. The functions are stored according to a hash of their
>> byte representation, so it's important that the function doesn't load
>> any additional code, changing its behavior without changing its
>> byte-string. Hence the use-case for something like CouchApp.
>
> The reason for the hashing is that we need to know when the function
> changes to know when to reindex documents. Storing library code
> somewhere ends up making this a bit more complicated. I wouldn't call
> it out of the question to have something, but I don't think its on
> anyone's agenda as things like CouchApp alleviate most of the need for
> it.

I see. This is quite clever hack. I like its simplicity (Couchdb
shines this way in other aspects as well, so kudos to all implementors
:-).
So the result of CouchApp push are just injected code chunks, right?
What about code size for really complicated macro sets, is it
irrelevant in practice?

Re: The sum function (used in reduce)

Posted by Paul Davis <pa...@gmail.com>.
On Sat, Apr 25, 2009 at 2:04 PM, Chris Anderson <jc...@apache.org> wrote:
> On Sat, Apr 25, 2009 at 11:05 AM, Wojciech Kaczmarek
> <ka...@gmail.com> wrote:
>> Now I see similar questions emerged. So my point is now: Is there a
>> way, existing or planned, to share some functions between views
>> without using couchapp macros?
>>
>
> No such plans. The functions are stored according to a hash of their
> byte representation, so it's important that the function doesn't load
> any additional code, changing its behavior without changing its
> byte-string. Hence the use-case for something like CouchApp.
>

The reason for the hashing is that we need to know when the function
changes to know when to reindex documents. Storing library code
somewhere ends up making this a bit more complicated. I wouldn't call
it out of the question to have something, but I don't think its on
anyone's agenda as things like CouchApp alleviate most of the need for
it.

HTH,
Paul Davis

> --
> Chris Anderson
> http://jchrisa.net
> http://couch.io
>

Re: The sum function (used in reduce)

Posted by Chris Anderson <jc...@apache.org>.
On Sat, Apr 25, 2009 at 11:05 AM, Wojciech Kaczmarek
<ka...@gmail.com> wrote:
> Now I see similar questions emerged. So my point is now: Is there a
> way, existing or planned, to share some functions between views
> without using couchapp macros?
>

No such plans. The functions are stored according to a hash of their
byte representation, so it's important that the function doesn't load
any additional code, changing its behavior without changing its
byte-string. Hence the use-case for something like CouchApp.

-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Re: The sum function (used in reduce)

Posted by Wojciech Kaczmarek <ka...@gmail.com>.
Hi!
Probably posting here for the first time..

On Wed, Apr 22, 2009 at 23:51, Nils Breunese <n....@vpro.nl> wrote:
> Adam Wolff wrote:
>
>> It'd be great to make it so that _design docs can define helper
>> functions too, so that two views can share a helper.
>
> You can already define functions in a separate file and use macros to
> 'import' them when defining view functions.

It's funny as couple of days ago I wanted to ask the list for the following:
Is there a way that one view can use output of another view, or at
least that views can share some common functions in one design doc?

Now I see similar questions emerged. So my point is now: Is there a
way, existing or planned, to share some functions between views
without using couchapp macros?

Re: The sum function (used in reduce)

Posted by Nils Breunese <n....@vpro.nl>.
Adam Wolff wrote:

> It'd be great to make it so that _design docs can define helper
> functions too, so that two views can share a helper.

You can already define functions in a separate file and use macros to  
'import' them when defining view functions.

Nils Breunese.

Re: The sum function (used in reduce)

Posted by Adam Wolff <aw...@gmail.com>.
It'd be great to make it so that _design docs can define helper
functions too, so that two views can share a helper.

A

On Wed, Apr 22, 2009 at 7:25 AM, Zachary Zolton
<za...@gmail.com> wrote:
> I've also expressed interest in both:
>
>  1) Adding/devising more helper functions
>
>  2) Upgrading SpiderMonkey to JavaScript version 1.8
>
> This would add a LOT features useful for succinct JavaScript Map/reduce code:
>  * More functional methods on Array objects, such as reduce()
>  * Destructuring assignments
>  * Short-cut Lambda notation (ooh la la!)
>
> Of course, SpiderMonkey 1.8 is still in RC1, however anyone who's been
> using Firefox 3 has been exposed to it!
>
> On Wed, Apr 22, 2009 at 9:00 AM, Thomas Heller <th...@gmail.com> wrote:
>> Hey,
>>
>> you can find it in COUCH_HOME/share/server/main.js
>>
>> sum = function(values) {
>>  var rv = 0;
>>  for (var i in values) {
>>    rv += values[i];
>>  }
>>  return rv;
>> }
>>
>> And no, its the only "helper" function.
>>
>> Regards,
>> /thomas
>>
>>
>> On Wed, Apr 22, 2009 at 3:55 PM, Rangachari Anand <rx...@gmail.com> wrote:
>>
>>> I searched all over the wiki and did not see any description of the
>>> sum function that can be called in the reduce method of
>>> a view. I can guess what it does but a pointer to more
>>> concrete information would be appreciated.
>>>
>>> There is only a passing reference to it in the page
>>> http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
>>>
>>> Are there any other handy functions that could be called from inside
>>> map or reduce?
>>>
>>> Thanks
>>> Rangachari Anand
>>>
>>
>

Re: The sum function (used in reduce)

Posted by Zachary Zolton <za...@gmail.com>.
I've also expressed interest in both:

 1) Adding/devising more helper functions

 2) Upgrading SpiderMonkey to JavaScript version 1.8

This would add a LOT features useful for succinct JavaScript Map/reduce code:
  * More functional methods on Array objects, such as reduce()
  * Destructuring assignments
  * Short-cut Lambda notation (ooh la la!)

Of course, SpiderMonkey 1.8 is still in RC1, however anyone who's been
using Firefox 3 has been exposed to it!

On Wed, Apr 22, 2009 at 9:00 AM, Thomas Heller <th...@gmail.com> wrote:
> Hey,
>
> you can find it in COUCH_HOME/share/server/main.js
>
> sum = function(values) {
>  var rv = 0;
>  for (var i in values) {
>    rv += values[i];
>  }
>  return rv;
> }
>
> And no, its the only "helper" function.
>
> Regards,
> /thomas
>
>
> On Wed, Apr 22, 2009 at 3:55 PM, Rangachari Anand <rx...@gmail.com> wrote:
>
>> I searched all over the wiki and did not see any description of the
>> sum function that can be called in the reduce method of
>> a view. I can guess what it does but a pointer to more
>> concrete information would be appreciated.
>>
>> There is only a passing reference to it in the page
>> http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
>>
>> Are there any other handy functions that could be called from inside
>> map or reduce?
>>
>> Thanks
>> Rangachari Anand
>>
>

Re: The sum function (used in reduce)

Posted by Jan Lehnardt <ja...@apache.org>.
Hey,

we recently changed things here. main.js is now generated
from a set of source files, just to keep things a little more
manageable. The definition of the sum() function now lives
in views.js. If you run `make` or `make install`, the contents
of view.js and the other files are copied into main.js. If you
change things in main.js, your changes will get lost the next
time you install CouchDB.

I agree that it is preferable to setup something more maintainable
in the long run, but the existing solution is good enough to
focus on other areas of work. :)

Cheers
Jan
--



On 22 Apr 2009, at 07:00, Thomas Heller wrote:

> Hey,
>
> you can find it in COUCH_HOME/share/server/main.js
>
> sum = function(values) {
>  var rv = 0;
>  for (var i in values) {
>    rv += values[i];
>  }
>  return rv;
> }
>
> And no, its the only "helper" function.
>
> Regards,
> /thomas
>
>
> On Wed, Apr 22, 2009 at 3:55 PM, Rangachari Anand  
> <rx...@gmail.com> wrote:
>
>> I searched all over the wiki and did not see any description of the
>> sum function that can be called in the reduce method of
>> a view. I can guess what it does but a pointer to more
>> concrete information would be appreciated.
>>
>> There is only a passing reference to it in the page
>> http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
>>
>> Are there any other handy functions that could be called from inside
>> map or reduce?
>>
>> Thanks
>> Rangachari Anand
>>


Re: The sum function (used in reduce)

Posted by Thomas Heller <th...@gmail.com>.
Hey,

you can find it in COUCH_HOME/share/server/main.js

sum = function(values) {
  var rv = 0;
  for (var i in values) {
    rv += values[i];
  }
  return rv;
}

And no, its the only "helper" function.

Regards,
/thomas


On Wed, Apr 22, 2009 at 3:55 PM, Rangachari Anand <rx...@gmail.com> wrote:

> I searched all over the wiki and did not see any description of the
> sum function that can be called in the reduce method of
> a view. I can guess what it does but a pointer to more
> concrete information would be appreciated.
>
> There is only a passing reference to it in the page
> http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
>
> Are there any other handy functions that could be called from inside
> map or reduce?
>
> Thanks
> Rangachari Anand
>