Lazy Iterators

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

Lazy Iterators

Artem Kobzar
The proposal based on really worst thing in JavaScript Arrays.

If i as developer want to make declarative code with combination of `Array#map`, `Array#filter` or `Array#reduce` - i will take a lot of iterations (count of chain element will determinate count of iterations).

```js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

const newArr = arr
  .map(a => a + 1)
  .filter(a => a % 2 === 0)
  .filter((a, i) => i < 3); // [2, 4, 6], but 3 iterations across `arr`
```

So, my proposal is: add lazy iterator with additional functions which help to use declarative collection method in chain as "zero-abstraction" and will iterate only once (will not be related to count of chain methods).

Actually, i've already created implementation and usage of this implementation looks like this:

```js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

const newArr = arr
  .lazy()
  .map(a => a + 1)
  .filter(a => a % 2 === 0)
  .take(3)
  .collect(); // [ 2, 4, 6 ], but 1 iterations across `arr`
```

And, actually, current implementation could be used with all `Iterable` objects, if this object will implement method `fromIterator` (it's needed for `LazyIterator#collect` method).

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

Re: Lazy Iterators

Jordan Harband

On Sat, Jul 27, 2019 at 1:45 PM Artem Kobzar <[hidden email]> wrote:
The proposal based on really worst thing in JavaScript Arrays.

If i as developer want to make declarative code with combination of `Array#map`, `Array#filter` or `Array#reduce` - i will take a lot of iterations (count of chain element will determinate count of iterations).

```js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

const newArr = arr
  .map(a => a + 1)
  .filter(a => a % 2 === 0)
  .filter((a, i) => i < 3); // [2, 4, 6], but 3 iterations across `arr`
```

So, my proposal is: add lazy iterator with additional functions which help to use declarative collection method in chain as "zero-abstraction" and will iterate only once (will not be related to count of chain methods).

Actually, i've already created implementation and usage of this implementation looks like this:

```js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

const newArr = arr
  .lazy()
  .map(a => a + 1)
  .filter(a => a % 2 === 0)
  .take(3)
  .collect(); // [ 2, 4, 6 ], but 1 iterations across `arr`
```

And, actually, current implementation could be used with all `Iterable` objects, if this object will implement method `fromIterator` (it's needed for `LazyIterator#collect` method).
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Lazy Iterators

Roma Bronstein
In reply to this post by Artem Kobzar
This was exactly what I proposed a few weeks ago, and received a not so enthusiastic response.
Which is strange, as I know many developers who complain exactly about this.

In it's current state, map/reduce/filter... functions are irrelevant performance wise in many scenarios in production environments.


On Sat, Jul 27, 2019, 23:45 Artem Kobzar <[hidden email]> wrote:
The proposal based on really worst thing in JavaScript Arrays.

If i as developer want to make declarative code with combination of `Array#map`, `Array#filter` or `Array#reduce` - i will take a lot of iterations (count of chain element will determinate count of iterations).

```js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

const newArr = arr
  .map(a => a + 1)
  .filter(a => a % 2 === 0)
  .filter((a, i) => i < 3); // [2, 4, 6], but 3 iterations across `arr`
```

So, my proposal is: add lazy iterator with additional functions which help to use declarative collection method in chain as "zero-abstraction" and will iterate only once (will not be related to count of chain methods).

Actually, i've already created implementation and usage of this implementation looks like this:

```js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

const newArr = arr
  .lazy()
  .map(a => a + 1)
  .filter(a => a % 2 === 0)
  .take(3)
  .collect(); // [ 2, 4, 6 ], but 1 iterations across `arr`
```

And, actually, current implementation could be used with all `Iterable` objects, if this object will implement method `fromIterator` (it's needed for `LazyIterator#collect` method).
_______________________________________________
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