Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A better future for comprehensions

dherman
June 05, 2014

A better future for comprehensions

My presentation to TC39 on deferring comprehensions to post-ES6 in order to simplify and generalize the feature, based on recent input from Jafar Husain.

dherman

June 05, 2014
Tweet

More Decks by dherman

Other Decks in Programming

Transcript

  1. A better future for
    comprehensions
    Dave Herman

    View full-size slide

  2. [for  (x  of  y)  if  (p(x))  f(x)]  
    (for  (x  of  y)  if  (p(x))  f(x))

    View full-size slide

  3. • Parallel JS is moving in the direction of parallel
    pipelines – a natural fit for comprehensions.
    • Three strikes and you refactor!
    • LINQ: one comprehension syntax, unbounded
    number of (user-definable) traversable datatypes.

    View full-size slide

  4. let  a  =  for  (x  of  a1)  
                       for  (y  of  a2)  
                           if  (y  >  x)  
                               {  x,  y  };

    View full-size slide

  5. let  i  =  for  (x  of  map1.keys())  
                       for  (y  of  map2.keys())  
                           if  (y  >  x)  
                               {  x,  y  };

    View full-size slide

  6. let  p  =  for  (x  of  a1.parallel())  
                       for  (y  of  a2.parallel())  
                           if  (y  >  x)  
                               {  x,  y  };

    View full-size slide

  7. • The LINQ idea (which is actually the Haskell idea):
    comprehensions desugar into generic combinators.
    • Any datatype that supports those combinators
    automatically gets to play along.

    View full-size slide

  8. • Defer comprehensions from ES6.
    • Jafar and I will present an ES7 proposal for
    generalized comprehensions.

    View full-size slide

  9. Generator
    Iterator
    .prototype
    .prototype
    [[Prototype]]
    generator
    [[Prototype]]
    [[Prototype]] ??

    View full-size slide

  10. Iterator.prototype.  
       zip  
       filter  
       map  
       ...

    View full-size slide

  11. table.keys().map(...)  
                           .filter(...)  
    !
    //  versus  
    !
    import  {  map,  filter  }  from  "itertools";  
    filter(map(table.keys(),  ...),  ...);

    View full-size slide

  12. (new  Iterator({  
       next()  {  ...  }  
    })).map(...)

    View full-size slide

  13. 5 Jun 14 Resolutions

    View full-size slide

  14. • Agree to defer comprehensions.
    • No future-proofing placeholder objects (can be
    added later with low compatibility risk).
    • May not generalize generator comprehensions
    since first RHS eagerly evaluated; more work to do.

    View full-size slide