natefaubion / sparkler

Native pattern matching for JavaScript
MIT License
694 stars 18 forks source link

Support generators? #11

Closed considerate closed 10 years ago

considerate commented 10 years ago

Is there any plan on supporting pattern matching for generator functions.

I would like this feature for easy creation of lazy functions acting on [x,...xs] pattern types of generators in order to use javascript in a more functional way when handling lists.

Proposed syntax:

function* square {
  [x,...xs] => {yield x*x; yield* square(xs)},
 * => undefined
}
var result = square([1,2,3,4,5,6]);
for(let value of result) {
   console.log(value);
}
/*
1
4
9
16
25
36
*/

Edit: Updated example with base case and expected result.

natefaubion commented 10 years ago

Sure, its a simple enough fix.

natefaubion commented 10 years ago

Added in 0.3.4