This proposal has not yet been presented to TC39 plenary meetings.
Python and Ruby support (first, *rest, last) = [1, 2, 3, 4]
, CoffeeScript support [first, rest..., last] = [1, 2, 3, 4]
, and Rust support [first, rest @ .., last] = [1, 2, 3, 4]
, all result in first
be 1
, last
be 4
, rest
be [2, 3]
. But surprisedly [first, ...rest, last] = [1, 2, 3, 4]
doesn't work in JavaScript.
And in some cases we really want to get the items from the end, for example getting matchIndex
from String.prototype.replace when using a function:
string.replace(pattern, (fullMatch, ...submatches, matchIndex, fullString) => {
// `matchIndex` is always the second to last param (the full string is the last param).
// There may be many submatch params, depending on the pattern.
})
A simple solution is making let [first, ...rest, last] = iterable
work as
let [first, ...rest] = iterable
let last = rest.pop()
The concern is it require save all items in rest
array, even you may only need last
. A possible mitigation is supporting [..., last] = iterable
which save the memory of rest
, but you still need to consume the entire iterator. In the cases which iterable
is a large array or something like Number.range(1, 100000)
, it's very inefficient. And in case like let [first, ..., last] = repeat(10)
(suppose repeat
is a generator returns infinite sequence of a same value), theoretically both first
and last
could be 10
.
Instead of simple solution, we could introduce double-ended iterator (like Rust std::iter::DoubleEndedIterator). A double-ended iterator could be consume from both ends.
let a = [1, 2, 3, 4, 5, 6]
let deiter = a.values() // suppose values() would be upgraded to return a double-ended iterator
deiter.next() // {value: 1}
deiter.next() // {value: 2}
deiter.next('back') // {value: 6}
deiter.next() // {value: 3}
deiter.next('back') // {value: 5}
deiter.next('back') // {value: 4}
deiter.next('back') // {done: true}
deiter.next() // {done: true}
With double-ended iterator, let [a, b, ..., c, d] = iterable
would roughly work as
let iter = iterable[Symbol.deIterator]()
let a = iter.next().value
let b = iter.next().value
let d = iter.next('back').value
let c = iter.next('back').value
iter.return()
To implement double-ended iterator in userland, we could use generator with function.sent
feature.
Array.prototype.values = function *values(array) {
for (let start = 0, end = array.length; start < end;) {
if (function.sent === 'back') yield array[--end]
else yield array[start++]
}
}
Double-ended iterator could have some extra iterator helpers like reversed
and reduceRight
.
DoubleEndedIterator.prototype.reversed = function *reversed() {
for (;;) {
let result
if (function.sent === 'back') result = this.next()
else result = this.next('back')
if (result.done) return result.value
else yield result.value
}
}
We could also easily have default implementation for reverse iterator if the object already support double-ended iterator.
Object.assign(X.prototype, {
*[Symbol.reverseIterator]() {
const iter = this[Symbol.deIterator]()
for (;;) {
let result
if (function.sent === 'back') result = iter.next()
else result = iter.next('back')
if (result.done) return result.value
else yield result.value
}
}
)
- Python iterable unpacking
- Ruby array decomposition
- CoffeeScript destructuring assignment with splats
- Rust subslice pattern
- Rust std::iter::DoubleEndedIterator
- Rust [Macro improved_slice_patterns::destructure_iter])(https://docs.rs/improved_slice_patterns/2.0.1/improved_slice_patterns/macro.destructure_iter.html)