New Set methods - again

Darien Valentine valentinium at gmail.com
Sun Jul 30 00:22:46 UTC 2017


> Sets are unordered, so actually having `forEach` (without the index
parameter) makes more sense than `reduce`.

When we work with sets, we do often treat them as unordered, like one would
in math I suppose, but the ES data structure does have a defined iteration
order, and its `forEach` method does call its callback with indices. Set
instances are given an internal `[[SetData]]` slot which is [defined as a
list](https://tc39.github.io/ecma262/#sec-set-iterable) and ["set
iterator"](https://tc39.github.io/ecma262/#sec-set-iterator-objects) uses
this and iterates in a deterministic order.

> `reduce` doesn't make sense for Sets because it is an ordered operation.

Ordered-ness of sets in ES aside, there’s nothing inherent to reduction
which makes it suitable only with ordered collections; consider the classic
reduce example, summing numbers.

Not that I’m advocating for or against the addition of reduce here, that
was just an example — just, the choices seemed to me a bit arbitrary.
(Generic iterator methods would indeed be a nice, except I’m not sure how
it would work since pushing another prototype into the chain between object
and array, object and set, object and map etc would be web-breaking.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170729/f02448ff/attachment.html>


More information about the es-discuss mailing list