Comprehensions, Where Art Thou?

Jason Orendorff jason.orendorff at gmail.com
Mon Aug 11 07:14:57 PDT 2014


On Mon, Aug 11, 2014 at 8:49 AM, Andy Wingo <wingo at igalia.com> wrote:
> Turns out, yes: you can do it by constructing a graph of combinators,
> with normal method syntax.  So you can do:
>
>   var sequence = iterable.lazy().if(somePredicate)
>
> where lazy and if might be:
>
>   Iterable.prototype.lazy = function* () {
>     for (var x of this) yield x;
>   }
>   Iterable.prototype.if = function* (pred) {
>     for (var x of this) if pred(x) yield x;
>   }

Dave Herman put together a repository showing a real program, adapted
from array comprehensions:
  https://github.com/dherman/sudoku/blob/master/solver.pythonic.js
to using methods called .lazy(), .concat(), .map(), .filter(), and .flatMap():
  https://github.com/dherman/sudoku/blob/master/solver.methods.js
but I don't understand what's going on there. Where are these methods
defined? Is it intended that .map() and .flatMap() will be defined on
all iterables?

If so, how? Didn't the committee also decide not to give Iterables or
Iterators a common prototype, or define these methods as part of the
Iterator or Iterable interface?

-j


More information about the es-discuss mailing list