15.4.4.21 Array.prototype.reduce ( callbackfn [ , initialValue [ , thisArg ] ] )

Brendan Eich brendan at mozilla.com
Sat Mar 21 13:52:00 PDT 2009


On Mar 21, 2009, at 10:26 AM, Edward Lee wrote:

> On Sat, Mar 21, 2009 at 11:57 AM, Brendan Eich <brendan at mozilla.com>  
> wrote:
>> Of course, all the other "array extras" are specified with a  
>> thisArg so
>> consistency argues that reduce and reduceRight should be specified  
>> that way
>> too.
>> I had to remind myself of Dave's analysis/argument, cited in full  
>> above, to
>> see why we didn't do that.
> What exactly was Dave's argument for not including a thisArg? There's
> a brief mention of multiple-optional-args-messiness, and then it talks
> about a future where other "array extras" don't have a thisArg.

Dave wrote:

> So this would mean reduce takes one callback argument and two  
> optional arguments: thisObject and init. Which one should come  
> first? The more common one is probably init, but then you separate  
> the callback arg from the "thisObject" arg, which is maybe okay.  
> Multiple optional arguments are kinda messy this way...


Indeed the other Array extras take (callback [, thisArg]) where []  
means optional, but reduce and reduceRight want an optional  
initialValue "fold basis". Without named parameters there's a problem,  
since the initialValue is probably more commonly supplied than thisArg  
(we intuit, and I think this is true from the reduce uses I've seen,  
but more data welcome).

We could surely give reduce the parameters (callback [, initialValue  
[, thisArg]]) but as Dave noted this breaks one symmetry (a more  
superficial kind than the loss of thisArg? nevertheless...) with the  
other Array extras, by not putting the optional thisArg parameter  
immediately after callback.

The other way 'round makes the initialValue parameter case have to  
supply a dummy thisArg, according to the hypothesis that initialValue  
is more commonly supplied in actual use cases.

Then there's the Pythonic argument: "just like Python => Python  
community mindshare". Of course Python's reduce is a function, not a  
method, but the mapping is straightforward.

/be


More information about the Es-discuss mailing list