tc39 / proposal-array-filtering Goto Github PK
View Code? Open in Web Editor NEWA proposal to make filtering arrays easier
Home Page: https://tc39.es/proposal-array-filtering/
License: MIT License
A proposal to make filtering arrays easier
Home Page: https://tc39.es/proposal-array-filtering/
License: MIT License
While I agree that the filter
method is very confusing, a thing I would hate more is having aliases. Array.prototype.filter
will most likely not be removed/deprecated when select
will make its way into JavaScript. Which will mean we will have 2 1:1 methods. My 2 cents are that I support this proposal only if it will ensure that programmers wont have to choose between filter
and select
.
In the presentation, I see the new name, but in the proposal repo, I don't see any mentioning of that. So, it's changed?
select
While there are some languages where term select
is synonymous with ECMAScript's filter
(i.e. Ruby), there are other languages where the term select
is synonymous with ECMAScript's map
:
As a heavy user of both C# and T-SQL (a SQL-92 derivative), I would find the term confusing. In the languages above, as well as other languages such as XQuery, the term where
is often synonymous with ECMAScript's filter
instead.
reject
I find the use of reject
here a possible source of confusion as the term is overloaded with Promise.reject
.
While I don't necessary find the use case for an alias of filter
compelling, if we did choose to add an alias then I might suggest some less ambiguous terms such as where
/whereNot
or where/exceptWhere
for these two member names.
Underscore Function | Count | Percentage vs filter |
---|---|---|
_.filter | 427,420 | |
_.select | 8,743 | 2% |
_.reject | 64,018 | 15% |
_.partition | 7,013 | 1.6% |
Using the following query format:
SELECT
COUNT(*)
FROM
`httparchive.response_bodies.2019_10_01_desktop`
WHERE
REGEXP_CONTAINS(body, r"_\.filter\(")
Sure, methods from this proposal should have equal on %TypedArray%
.
The question: how should work %TypedArray%.prototype.groupBy
?
I think it'd be great if, prior to Stage 2 (or at least Stage 3), this could be prototyped as an independent package on npm, so we can get some real-world feedback on how people like it.
IMO, it would be best to do this as a module exporting these two things as functions, rather than monkey-patching onto the Array.prototype, for the reasons described in Andrew Betts' essay Polyfills and the evolution of the Web.
Even if similar features are available elsewhere, and we have a good understanding from that experience that it would be a good idea, this prototype may give us some more data on the details we select in this proposal.
I meant to find the tuples with a difference, but wrote it backwards again!
$$('head meta')
.map(m => [m.content === m.getAttribute('content'), m.content, m.getAttribute('content')])
.filter(t => t[0]) // should be `!t[0]`
Not sure if it is the right place to suggest this, but.
Sometimes you don't want to alloc another array with .filter
. It is either when you want to avoid an unnecessary allocation, or when you want to free a garbage collector from doing unnecessary work.
xs.map(f1).filter(f2)...
It is still a linear time operation:
function filterInPlace(xs, predicate) {
let writeIndex = 0;
for (let readIndex = 0; readIndex < xs.length; readIndex++) {
if (predicate(xs[readIndex], readIndex, xs) {
xs[writeIndex++] = xs[readIndex];
}
}
xs.length = writeIndex;
}
In spirit of this proposal one would also have to add rejectInPlace
with predicate
used to reject elements instead of keeping them.
It would be nice if something like that were in the standard. Probably on the array prototype, but anything goes I guess.
As all ES6+ methods, those methods should be added to Array.prototype[@@unscopables]
. It should be added to the spec draft.
In the slides for this meeting, there is a request for a new proposal, groupBy, to be moved to stage 2 as well. However I am unable to find spec text for it. Is it published anywhere?
Basics:
const a = 1, b = 2, c = 3, d = 4
const [ab, cd] = [a,b,c,d].partition((_, index) => index >= 2)
const [ac, bd] = [a,b,c,d].partition(value => value % 2 === 0)
Immutable (out of place) Quick Sort:
function quickSort([p, ...tail]) {
if (!tail.length) return [p, ...tail]
const [l, r] = tail.partition(x => x > p)
return [...quickSort(l), p, ...quickSort(r)]
}
quickSort([5, 2, 3, -1])
Efficient and correct implementation usually not trivial:
// not trivial
function partition(arr, callback) {
return arr.reduce((result, value, index, array) => {
result[callback(value, index, array) ? 1 : 0].push(value);
return result;
}, [[], []]);
}
or
// not efficient (twice linear scan)
function partition(arr, callback) {
return [
arr.filter((...args) => !callback(...args)),
arr.filter(callback)
];
}
or based this proposal:
function partition(arr, callback) {
return [
arr.filterOut(callback),
arr.filter(callback)
];
}
OCaml: List.partition
Haskell: Data.List.partition
Lodash: _.partition
RxJS: RxJS/partition
Kotlin: Array.partition
With flatMap
added with flat
to reduce iterations and allocations, it would be nice to have an accompanying filterMap
method as well. I often find filter and map used together.
Something like this:
[{active: true, label: "foo"}, {active: true, label: "bar"}]
.filter(v => v.active)
.map(v => v.label)
Could be written as:
[{active: true, label: "foo"}, {active: true, label: "bar"}]
.filterMap(v => v.active && v.label)
Hi! As part of proposal-change-array-by-copy we have been discussing not consulting Symbol. Species
when constructing the return values.
tc39/proposal-change-array-by-copy#33
There hasn't been new methods added to Array since https://github.com/tc39/proposal-rm-builtin-subclassing was presented so the next proposal that does may set a precedence.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.