On Sun, Mar 25, 2018 at 6:20 PM, 月の影 <19511...@qq.com> wrote: > Sometimes I got a infinity iterable sequences, I may want a partial spreed > syntax. `...iterableObject{from, to}` > > For example: > > ```js > 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) > > ```js > 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())]); ``` ~TJ _______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss