Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29018 > unrolled thread

ES6 itertools

Started byAsen Bozhilov <asen.bozhilov@gmail.com>
First post2015-12-16 22:30 +0200
Last post2015-12-22 08:37 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  ES6 itertools Asen Bozhilov <asen.bozhilov@gmail.com> - 2015-12-16 22:30 +0200
    Re: ES6 itertools Scott Sauyet <scott.sauyet@gmail.com> - 2015-12-22 08:37 -0800

#29018 — ES6 itertools

FromAsen Bozhilov <asen.bozhilov@gmail.com>
Date2015-12-16 22:30 +0200
SubjectES6 itertools
Message-ID<n4shlq$huj$1@solani.org>
I am working on project called ES-Iter <URL: 
https://github.com/abozhilov/ES-Iter>. 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.

[toc] | [next] | [standalone]


#29029

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-12-22 08:37 -0800
Message-ID<1870a461-01b3-4962-b6c6-d2dbcdc86bd7@googlegroups.com>
In reply to#29018
Asen Bozhilov wrote:
> I am working on project called ES-Iter 
>     <URL: https://github.com/abozhilov/ES-Iter>. 
> [ ... ]
> 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.com/lazy.js/
  [2]: https://github.com/goatslacker/lz
  [3]: https://github.com/fantasyland/fantasy-land
  [4]: https://github.com/abozhilov/ES-Iter#mapcallback--x--x
  [5]: https://github.com/fantasyland/fantasy-land#functor
  [6]: http://ramdajs.com/

  -- Scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web