Code Monkey home page Code Monkey logo

proposal-array-filtering's People

Contributors

jridgewell avatar numminorihsf avatar sosukesuzuki avatar zloirock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

proposal-array-filtering's Issues

Alias problem

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.

.filterReject or .filterOut?

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?

Concern with terms "select" and "reject"

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.

Query Data from the HTTP Archive and GitHub Archive

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\(")

%TypedArray% methods

Sure, methods from this proposal should have equal on %TypedArray%.

The question: how should work %TypedArray%.prototype.groupBy?

Prototype as independent npm package for feedback?

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 did it again!

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]`

In place

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.

  • when the original array is large and you don't care about the unfiltered version
  • when you have to filter a lot of small arrays and you don't care about the unfiltered version
  • when you do a chain like 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.

@@unscopables

As all ES6+ methods, those methods should be added to Array.prototype[@@unscopables]. It should be added to the spec draft.

is this a joke?

  • Name one programming language that has a different interface for filter.
  • That is because filter by definition returns a subset of elements of the given data structure, for which a given predicate returns true.

groupBy spec text

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?

Suggestion to add Array#partition as well

Examples

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)
  ];
}

Related

OCaml: List.partition
Haskell: Data.List.partition
Lodash: _.partition
RxJS: RxJS/partition
Kotlin: Array.partition

Array#filterMap

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.