Last active
October 28, 2024 12:13
-
-
Save mtavkhelidze/a6191a85670bcff9e1472840add4b5a6 to your computer and use it in GitHub Desktop.
Fun with lazy evaluation in JavaScript.
This file contains 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
/** | |
* @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