Re: Set.prototype.entries: indices as keys?
Dmitry Soshnikov wrote: ```js let [x,y] = set; // x='a'; y='b’; ``` Would this work actually? :) Destructuring does get property, which wouldn't call set's `get` method. See http://people.mozilla.org/~jorendorff/es6-draft.html#sec-runtime-semantics-destructuringassignmentevaluation -- destructuring array patterns uses the iteration protocol, which is why the line works as Axel suggested: js let set = new Set(['a', 'b']); js let [x,y] = set; js console.log(x, y) a b js for (let elt of set) console.log(elt) a b But not all iterables are indexable, nor should they be. /be ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Set.prototype.entries: indices as keys?
Sets are not a linear data structure, so the order of entries in a Set is irrelevant, unlike an Array whose elements must have an explicit position. Set entries in JS have an iteration order purely to match programmer intuition (and ensure that all implementations adhere), however there is no structural importance of the order. You can impose an order on the elements of a set, though. For example, Java’s `SortedSet` [1] has the methods `first()` and `last()`, which are occasionally useful. I agree that it feels weird to have indices attached to set elements, but it would at least make `entries()` useful. You could iterate and treat the first element and/or the last element differently. But the same can be achieved by iterating over a `zip()` of a set and a `range()` (assuming iterable-based tool functions `zip` and `range`). [1] http://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html -- Dr. Axel Rauschmayer a...@rauschma.de rauschma.de ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Set.prototype.entries: indices as keys?
Currently the keys of the entries returned by `Set.prototype.entries()` are the same as the values: ```js let set = new Set(['a', 'b']); let pairs = [...set.entries()]; console.log(JSON.stringify(pairs)); // [[a,a],[b,b”]] ``` Given that sets are ordered, I’d use the “position” of an entry as the key: [[0,a],[1,b”]] Rationale: First, having an indices as keys makes the entries more useful. Second, destructuring already treats entries as if they had indices: ```js let [x,y] = set; // x='a'; y='b’; ``` -- Dr. Axel Rauschmayer a...@rauschma.de rauschma.de ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Set.prototype.entries: indices as keys?
+1 --- R. Mark Volkmann Object Computing, Inc. On Jan 18, 2015, at 6:28 AM, Axel Rauschmayer a...@rauschma.de wrote: Currently the keys of the entries returned by `Set.prototype.entries()` are the same as the values: ```js let set = new Set(['a', 'b']); let pairs = [...set.entries()]; console.log(JSON.stringify(pairs)); // [[a,a],[b,b”]] ``` Given that sets are ordered, I’d use the “position” of an entry as the key: [[0,a],[1,b”]] Rationale: First, having an indices as keys makes the entries more useful. Second, destructuring already treats entries as if they had indices: ```js let [x,y] = set; // x='a'; y='b’; ``` -- Dr. Axel Rauschmayer a...@rauschma.de rauschma.de ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss