partial spread syntax
On Sun, Mar 25, 2018 at 6:20 PM, 月の影 <19511344 at qq.com> wrote:
Sometimes I got a infinity iterable sequences, I may want a partial spreed syntax.
...iterableObject{from, to}
For example:
function *fibonacci() { let a = 0, b = 1 while(1) { [a, b] = [b, a + b] yield a } } console.log([...fibonacci(){3, 5}]) // [3, 5, 8]
For a finite list, it is similar to [...list].slice(from, to)
const arr1 = [1, 2, 3, 4], arr2 = [5, 6, 7, 8] console.log([...arr1{2}, ...arr2{1}]) // 3, 4, 6, 7, 8
This seems to be well-served by a simple library function right now:
function take(n, iter) {
if(n <= 0) return;
for(let item of iter) {
yield item;
if(n-- <= 0) return;
}
}
function drop(n, iter) {
iter = iter[Symbol.iterator];
for(let i = 0; i < n; i++) {
iter.next();
}
yield* iter;
}
function subiter(start, end, iter) {
return take(end-start+1, drop(start, iter));
}
console.log([...subiter(3, 5, fibonacci())]);
On Sun, Mar 25, 2018 at 9:20 PM, 月の影 <19511344 at qq.com> wrote:
Sometimes I got a infinity iterable sequences, I may want a partial spreed syntax.
...iterableObject{from, to}
How would you prevent ambiguity between the spread and blocks?
iterable { from, to };
is currently equivalent to
iterable; { from, to; }
I'm not sure I'd prefer this over just using a function on the Array prototype... you aren't saving too many characters and are adding some ambiguity with to {} usage. I don't like it.
Sometimes I got a infinity iterable sequences, I may want a partial spreed syntax.
...iterableObject{from, to}
For example:
function *fibonacci() { let a = 0, b = 1 while(1) { [a, b] = [b, a + b] yield a } } console.log([...fibonacci(){3, 5}]) // [3, 5, 8]
For a finite list, it is similar to [...list].slice(from, to)
const arr1 = [1, 2, 3, 4], arr2 = [5, 6, 7, 8] console.log([...arr1{2}, ...arr2{1}]) // 3, 4, 6, 7, 8