Map: filter/map and more

Domenic Denicola d at domenic.me
Thu Nov 20 14:48:50 PST 2014


From: Tab Atkins Jr. [mailto:jackalmage at gmail.com] 

> Interesting idea, but I'm not sure how it's supposed to work in practice.

Doesn't answer all of your questions, and is probably half-assed in various ways, but here is some stuff that we came up with in some live-coding during the meeting:

```js
Map.prototype.entries = function () {
  return new MapIterator({ collectAs: this.constructor[Symbol.species] });
};
class MapIterator extends Iterator {
  constructor({ collectAs }) {
    super();
    this[Symbol.collectAs] = collectAs;
  }
}
Iterator.prototype.collect = function () {
  return this[Symbol.collectAs].from(...);
};
myMap.entries()                      // proposal: you can remove .entries()
  .filter(([k, v]) => k % 2 === 0)
  .map(([k, v]) => [k * 2, v])
  .forEach(...);
  // or .reduce(...) also forces
  // or for-of
  // or .collectAs(Array)
  // or .collect(), which uses [Symbol.collectAs] as a default
```


More information about the es-discuss mailing list