function sequence(start, step) {
var x = 0;
return function () {
start = start || 0;
step = step || 1;
var y = start + (step * x)
x++;
return y;
}
}
function take(gen, x) {
var arr = [];
for (var i = 0; i < x; i++) {
arr[i] = gen();
};
return arr;
}
var gen2 = sequence(0, 2);
console.log(take(gen2, 5)); // [0, 2, 4, 6, 8 ]
ZnVuY3Rpb24gc2VxdWVuY2Uoc3RhcnQsIHN0ZXApIHsKCXZhciB4ID0gMDsKCXJldHVybiBmdW5jdGlvbiAoKSB7CgkJc3RhcnQgPSBzdGFydCB8fCAwOwoJCXN0ZXAgPSBzdGVwIHx8IDE7CgkJdmFyIHkgPSBzdGFydCArIChzdGVwICogeCkKCQl4Kys7CgkJcmV0dXJuIHk7Cgl9Cn0KCgpmdW5jdGlvbiB0YWtlKGdlbiwgeCkgewoJdmFyIGFyciA9IFtdOwoJZm9yICh2YXIgaSA9IDA7IGkgPCB4OyBpKyspIHsKCQlhcnJbaV0gPSBnZW4oKTsKCX07CglyZXR1cm4gYXJyOwp9CgoKCnZhciBnZW4yID0gc2VxdWVuY2UoMCwgMik7CmNvbnNvbGUubG9nKHRha2UoZ2VuMiwgNSkpOyAvLyBbMCwgMiwgNCwgNiwgOCBd