Pure win: Array.from and Array.of

Dmitry A. Soshnikov dmitry.soshnikov at gmail.com
Sun Jul 10 12:38:46 PDT 2011


On 10.07.2011 23:09, Dmitry A. Soshnikov wrote:
> And by the way, an efficient `Array.prototype.unique` also would be 
> nice to have, since in JS in general it's hard to implement it's 
> efficiently (in lower level at least it will iterate faster).
>
> [1, 3, 2, 5, 5, 3].unique(); // [1, 3, 2, 5]
>
> Dmitry.
>
> On 10.07.2011 23:02, Dmitry A. Soshnikov wrote:
>> On 10.07.2011 22:44, Brendan Eich wrote:
>>> On Jul 10, 2011, at 10:40 AM, Dmitry A. Soshnikov wrote:
>>>
>>>> On 10.07.2011 21:23, 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.
>>>>>
>>>>
>>>> You mean when `Array` itself is passed as an argument?
>>>>
>>>> var o = (function (ArrayConstructor, ...rest) {
>>>>     return ArrayConstructor(...rest);
>>>> })(Array, 10, 20, 30);
>>>
>>> Yes. Now consider the case where you leave out the 20 and 30.
>>>
>>
>> return ArrayConstructor(rest[0]) ?
>>

Ah, goddammit, sorry, I got it. Completely forgot about this case, even 
if myself just showed it before in Array(42).map example, thanks for 
mentioning. It will of course create array with length 10 and with holes 
(some implementations, in particular V8 even pre-allocate these holes).

So Array.of is just exactly and only for this use-case.

Dmitry.

>> May I ask to show nevertheless how you want to apply here Array.of?
>>
>> P.S.:
>>
>> If this is a wish-list of extending standard array lib, we can 
>> consider also the following:
>>
>> - Array.prototype.remove(value, all)
>>
>> [1, 2, 3, 2].remove(2); // [1, 3, 2]
>> [1, 2, 3, 2].remove(2, true); // [1, 3]
>>
>> (seems this function is required more than Array.of, because at least 
>> I saw it implemented in all frameworks and used it myself).
>>
>> - Array.prototype.subtract(array)
>>
>> [1, 2, 3, 4].subtract([2, 4]); // [1, 3]
>>
>> - Array.seq(from, to)
>>
>> Array.seq(1, 5); // [1, 2, 3, 4, 5]
>>
>> - Array.min(array), Array.max(array) (can be implemented with 
>> Math.max/min and apply though)
>>
>> Array.min = (array) -> Math.min.apply(Math, array)
>>
>> - Array.prototype.split(n)
>>
>> ["a", "b", "c", "d", "e"].split(3) // [["a", "b", "c"], ["d", "e", "f"]]
>>
>> Perhaps even to build objects from lists of keys and values (this 
>> function is usually called as `zip`):
>>
>> - Object.fromLists(["a", "b", "c"], [1, 2, 3]); // {a: 1, b: 2, c: 3}
>>
>> Dmitry.
>

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


More information about the es-discuss mailing list