partial spread syntax

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

partial spread syntax

月の影
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 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
```

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: partial spread syntax

Tab Atkins Jr.
On Sun, Mar 25, 2018 at 6:20 PM, 月の影 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: partial spread syntax

Mike Samuel
In reply to this post by 月の影


On Sun, Mar 25, 2018 at 9:20 PM, 月の影 <[hidden email]> 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;
}

 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: partial spread syntax

Michael J. Ryan
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 regards to {} usage.  I don't like it.

--
Michael J. Ryan - http://tracker1.info


On Mon, Mar 26, 2018 at 5:25 AM Mike Samuel <[hidden email]> wrote:


On Sun, Mar 25, 2018 at 9:20 PM, 月の影 <[hidden email]> 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;
}

 
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss