Skip to content

Instantly share code, notes, and snippets.

@igoratron
Created February 9, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igoratron/25aa72a0f992ca226f84 to your computer and use it in GitHub Desktop.
Save igoratron/25aa72a0f992ca226f84 to your computer and use it in GitHub Desktop.
lazy underscore kata
var generators = {
range: function *rangeGenerator(start, end, step) {
for(var i = start; i < end; i += step) {
console.log('generating', i);
yield i;
}
},
take: function *takeGenerator(iter, n) {
for(var i = 0; i < n; i += 1) {
yield iter.next().value;
}
},
filter: function *filterGenerator(iter, cb) {
for(var item of iter) {
if(cb(item)) {
yield item;
}
}
}
}
var functions = {
range: function range(start, end, step) {
var it = generators.range(start, end, step);
var ret = Object.create(functions);
ret.value = it;
return ret;
},
take: function take(n) {
var it = generators.take(this.value, n)
var ret = Object.create(functions);
ret.value = it;
return ret;
},
filter: function filter(cb) {
var it = generators.filter(this.value, cb)
var ret = Object.create(functions);
ret.value = it;
return ret;
}
}
var a = functions.range(0, 10, 1)
.filter((number) => number % 2 === 0)
.take(2)
.value;
for(var el of a) {
console.log('result', el);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment