Skip to content

Instantly share code, notes, and snippets.

@PaulMaynard
Last active May 2, 2016 16:13
Show Gist options
  • Save PaulMaynard/c4c509889ba8869bf320 to your computer and use it in GitHub Desktop.
Save PaulMaynard/c4c509889ba8869bf320 to your computer and use it in GitHub Desktop.
Generator and Iterator manipulation
/**
* Usage: _(_.filter(_.map(_.range(1, 10), x => x * x), x => x % 2 === 0)) // Even squares of numbers between 1-100
*/
(function(){'use strict';
var _ = window._ = window.eater = it => [...it];
Object.assign(_, {
*range(start, end) {
if(end == null) {
[start, end] = [0, start];
}
for(var i = start; i <= end; i++) {
yield i;
}
},
*map(it, fun) {
for(var i of it) {
yield fun(i);
}
},
*filter(it, fun) {
for(var i of it) {
if(fun(i)) {
yield i;
}
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment