Skip to content

Instantly share code, notes, and snippets.

@mtavkhelidze
Last active October 29, 2019 08:39
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 mtavkhelidze/a6191a85670bcff9e1472840add4b5a6 to your computer and use it in GitHub Desktop.
Save mtavkhelidze/a6191a85670bcff9e1472840add4b5a6 to your computer and use it in GitHub Desktop.
Some fun with lazy evaluation in JavaScript.
/**
* @constructor
*/
function* range(from, until) {
let i = from;
const stop = until ? i => i >= until : () => false;
while (true) {
if (!stop(i)) {
yield i;
} else {
return i;
}
i += 1;
}
}
range.prototype.take = function (n) {
const res = [];
for (let i = 0; i < n; i++) {
const { value, done } = this.next();
res.push(value);
if (done) {
break;
}
}
return res;
};
range.prototype.toArray = function () {
const res = [];
while (true) {
const { value, done } = this.next();
if (value) {
res.push(value);
}
if (done) {
break;
}
}
return res;
};
range.prototype.withMap = function (fn) {
return {
next: () => {
const { value, done } = this.next();
return { value: fn(value), done };
},
...api,
[Symbol.iterator]: () => {
return this;
},
};
};
range.prototype.withFilter = function (fn) {
return {
next: () => {
while (true) {
const { value, done } = this.next();
if (fn(value)) {
return { value, done };
}
}
},
...api,
[Symbol.iterator]: () => {
return this;
},
};
};
const api = {
take: range.prototype.take,
toArray: range.prototype.toArray,
withMap: range.prototype.withMap,
withFilter: range.prototype.withFilter,
};
let xs = range(1, 10).toArray();
console.log(xs);
xs = range(1).withFilter(x => x % 13 === 0).take(10);
console.log(xs);
xs = range(1).withMap(x => x * x * x).take(10);
console.log(xs);
xs = range(1).withFilter(x => x % 13 === 0).withMap(x => x * x * x).take(5);
console.log(xs);
xs = range(1).withFilter(x => x % 13 === 0).withMap(Math.sqrt).take(5);
console.log(xs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment