[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

ES6 itertools

Asen Bozhilov

12/16/2015 8:31:00 PM

I am working on project called ES-Iter <URL:
https://github.com/abozhilov/E.... Since, ES6 provides iterator
protocol and `for-of` loop the intention of lib is to provide set of
utility functions for lazy iteration in similar way of Python's itertools.

The lib is inspired by Python, but it is not 1 to 1 port.

Some design considerations:

- Error detection is as early as it can, e.g. `let z = Iter.zip([1, 2,
3], null); for (let i of z) {}` it will throw an error in time of
calling `zip` not when the iteration starts.

- Every iterator is closed properly on abrupt exits e.g. break, return,
throw. This is really important, especially for some host objects which
could implement iterator protocol. E.g. File object can implement
iterator protocol and in their `return` method must close the file
properly.

- The most common used method are implemented as static.

- Every method returns new Iter instance in order to allow chaining
calls e.g. `Iter.range(10).filter((x) => x % 2).accumulate()`

I am open for discussion, and contributions. Any improvements are welcome.
2 Answers

ram

12/16/2015 8:51:00 PM

0

Asen Bozhilov <asen.bozhilov@gmail.com> writes:
>I am working on project called ES-Iter <URL:
>https://github.com/abozhilov/E.... Since, ES6 provides iterator
>protocol

It's actually called »Iterator /interface/« in 25.1.1.2.

Scott Sauyet

12/22/2015 4:38:00 PM

0

Asen Bozhilov wrote:
> I am working on project called ES-Iter
> <URL: https://github.com/abozhilov/E....
> [ ... ]
> I am open for discussion, and contributions. Any improvements are
> welcome.

How different do you find this from libraries such as Lazy [1] or
lz.js [2]?

I think it would be useful if you could support as much of the
FantasyLand specification [3] as possible. For instance, your `map`
method [4] looks almost certain to support FantasyLand's Functor laws
[5], so an Iter instance should be a Functor, and can be manipulated
like any other Functor. But if you also supplied `chain` (it looks
like your `flatMap` with the `deep` parameter set to false) you could
support another of the specifications. You could probably support many
of the specifications of FantasyLand.

FantasyLand has been growing as a way to describe abstract, algebraic
types.

From my perspective as one of the authors of Ramda [6], this would
make working with Iter very easy:


const R = require('ramda'); // general-purpose utility lib
const Maybe = require('data.maybe'); // Folktale Maybe (is Functor)

// All are functors
R.map(square, [1, 2, 3, 4, 5]); //=> [1, 4, 9, 16, 25]
R.map(square, Maybe.Just(7)); //=> Maybe.Just(49)
R.map(square, Maybe.Nothing); //=> Maybe.Nothing
R.map(square, new Iter([1, 2, 3]); //=> ~ new Iter([1, 4, 9])

// But other Ramda integration should also work
let fibo = new Iter(function* () {
let [a, b] = [0, 1];
while(true) {
yield a;
[a, b] = [b, a + b]
}
});

R.compose(R.take(6), square)(fibo);
//=> [0 * 0, 1 * 1, 1 * 1, 2 * 2, 3 * 3, 5 * 5]
//~~> [0, 1, 1, 4, 9, 25]


Note that Ramda's delegation to your code is independent of your
implementing the FantasyLand specifications, but if you do
implement them, then certain advanced behavior automatically
applies. And people could use Iter with other common tools that
know the specification, but not the details of your API.


[1]: http://danieltao.co...
[2]: https://github.com/goat...
[3]: https://github.com/fantasyland/fa...
[4]: https://github.com/abozhilov/ES-Iter#mapcall...
[5]: https://github.com/fantasyland/fa...#functor
[6]: http://ra...

-- Scott