Pure win: Array.from and Array.of
Dmitry A. Soshnikov
dmitry.soshnikov at gmail.com
Sun Jul 10 12:02:09 PDT 2011
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]) ?
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/360aee05/attachment.html>
More information about the es-discuss
mailing list