Last active
May 2, 2016 16:13
-
-
Save PaulMaynard/c4c509889ba8869bf320 to your computer and use it in GitHub Desktop.
Generator and Iterator manipulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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