Pure win: Array.from and Array.of

Dmitry A. Soshnikov dmitry.soshnikov at gmail.com
Sun Jul 10 13:00:08 PDT 2011


On 10.07.2011 23:25, Juan Ignacio Dopazo wrote:
>
> On Sun, Jul 10, 2011 at 2:35 PM, Dmitry A. Soshnikov 
> <dmitry.soshnikov at gmail.com <mailto:dmitry.soshnikov at gmail.com>> wrote:
>
>     On 10.07.2011 21:32, Allen Wirfs-Brock wrote:
>>
>>     On Jul 10, 2011, at 10:23 AM, Brendan Eich wrote:
>>
>>>     On Jul 10, 2011, at 10:18 AM, Rick Waldron wrote:
>>>
>>>>     The more I think about it, I still can't come up with any
>>>>     really exciting use cases where Array.of
>>>>     <http://Array.of/> would outshine anything that already exists.
>>>>     I say strike it from the wishlist.
>>>
>>>     Higher-order programming with Array as constructing-function
>>>     bites back for the single-number-argument case. That's where
>>>     Array.of helps.
>>
>>     Yes, if you actually need to pass Array.of as a function
>>     argument.  Of course if we have block lambdas you could just say:
>>
>>          hof({|a|[a]})
>>     instead of
>>          hof(Array.of)
>>
>
>     Can you show a real use-case of it please?
>
>     Dmitry.
>
>
> A real use-case for this functions: 
> http://yuilibrary.com/forum/viewtopic.php?f=18&t=7978 
> <http://yuilibrary.com/forum/viewtopic.php?f=18&t=7978>
> YUI uses get/set methods for dealing with attributes and has a 
> declarative form for them that looks like this:
>
> Y.SomeClass = Y.Base.create('someClass', Y.Superclass, [Y.Mixin], proto, {
>   ATTRS: {
>     someAttr: {
>       value: []
>     }
>   }
> });
>
> Using [] as a default value can lead to bugs because the class will 
> hold a reference to a unique array in all instances. Using a 
> constructor function one can be sure each instance will have a fresh 
> array.
>
> Y.SomeClass = Y.Base.create('someClass', Y.Superclass, [Y.Mixin], proto, {
>   ATTRS: {
>     someAttr: {
>       valueFn: Array
>     }
>   }
> });
>
> Here valueFn calls the provided function to create the default value 
> each time. In this case it would create a new array. Array creation 
> functions would help create more complex patters and avoid bugs with 
> unexpected parametes (tough now I think Array.bind(null, 0) maybe 
> could work...
>

I see. Though, the case with `Array.bind(null, 0)` seems is particular 
for YUI.

Anyway, from what I can say now `Array.of` is a "fixed" version of 
`Array` which doesn't have a special case of `Array(length)`, but always 
considers it as Array(items...). Don't know how rare/often this case is. 
Sometimes it's needed vice-versa to create Array(length). I also think 
we should consider other proposals of arrays library which I mentioned 
in previous letter.

Dmitry.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110711/ff046dfc/attachment.html>


More information about the es-discuss mailing list