Proposal: Static sort method on Array
kai zhu
kaizhu256 at gmail.com
Sun Apr 8 03:47:33 UTC 2018
if you want to adhere to the python/jslint philosophy of “there should be one and preferably only one common design-pattern to do it”, then array.from is the most suitable candidate for copying/coercing lists. it can generalise to common pseudo-lists like function-arguments and frontend-query-selectors, which array.slice cannot as shown in these real world examples [1] [2]:
```js
/*
* coerce/copy frontend query-selector pseudo-list to list
*/
// disable <script> tag
Array.from(
document.querySelectorAll('script')
).forEach(function (element) {
element.outerHTML = '<script></script>';
});
```
```js
/*
* coerce/copy function-argument pseudo-list to list
*/
task.onDone = function () {
...
// preserve error.message and error.stack
task.result = JSON.stringify(Array.from(arguments)
.map(function (element) {
if (element && element.stack) {
element = local.objectSetDefault(local.jsonCopy(element), {
message: element.message,
name: element.name,
stack: element.stack
});
}
return element;
}));
```
[1] https://github.com/kaizhu256/node-utility2/blob/2018.1.13/lib.utility2.js#L2781 <https://github.com/kaizhu256/node-utility2/blob/2018.1.13/lib.utility2.js#L2781>
[2] https://github.com/kaizhu256/node-utility2/blob/2018.1.13/lib.utility2.js#L5844 <https://github.com/kaizhu256/node-utility2/blob/2018.1.13/lib.utility2.js#L5844>
> On 8 Apr 2018, at 11:12 AM, Naveen Chawla <naveen.chwl at gmail.com> wrote:
>
> `slice()` is better than `Array.from()` if you already have an array because you can chain it with the other Array.prototype methods.
>
> Good point about not needing it after you've done a map/filter/concat or whatever, since you already have a new array.
>
> However I agree with the thrust of a proposal that produces a new array from sort instead of in-place, at least from when `sort` was being introduced.
> I have made bugs on this presumption with sort(), until I learned it is in-place.
>
> However, since sort() exists as it is now, it could be too confusing to have 2 `sort`s in JavaScript. If this is the case, we may have to accept this as a JavaScript language mistake in hindsight that we have to work around using slice(), specific to sort (but not the other Array.prototype methods).
>
> But, if it's not too confusing, then I would have no problem with e.g.:
>
> `Array.prototype.sortedShallowClone`
>
> being introduced to the language.
>
> On Sun, 8 Apr 2018 at 03:48 T.J. Crowder <tj.crowder at farsightsoftware.com <mailto:tj.crowder at farsightsoftware.com>> wrote:
> On Sat, Apr 7, 2018 at 8:59 PM, Rob Ede <robjtede at icloud.com <mailto:robjtede at icloud.com>> wrote:
> > ...I'm considering creating a proposal to add an Array.sort()
> > method that takes an array and returns a new array...
>
> That would be:
>
> ```js
> let newArray = originalArray.slice().sort();
> // or
> let newArray = Array.from(originalArray).sort();
> // or
> let newArray = [...originalArray].sort();
> ```
>
> I don't know that we need a new static for it. Unless the motivation is to allow insertion sort or other sort algorithms that work best when creating a new array as a result? But if we assume `Array.prototype.sort` already uses quicksort or mergesort or similar, I'm not seeing much reason to add a new static to allow insertsion sort or similar...
>
> Can you expand on use cases and why the above aren't sufficient?
>
> -- T.J. Crowder
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180408/b037a764/attachment-0001.html>
More information about the es-discuss
mailing list