Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy evaluation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====JavaScript==== In [[JavaScript]], lazy evaluation can be simulated by using a [[Generator (computer programming)|generator]]. For example, the [[Stream (computing)|stream]] of all [[Fibonacci numbers]] can be written, using [[memoization]], as: <syntaxhighlight lang="js"> /** * Generator functions return generator objects, which reify lazy evaluation. * @return {!Generator<bigint>} A non-null generator of integers. */ function* fibonacciNumbers() { let memo = [1n, -1n]; // create the initial state (e.g. a vector of "negafibonacci" numbers) while (true) { // repeat indefinitely memo = [memo[0] + memo[1], memo[0]]; // update the state on each evaluation yield memo[0]; // yield the next value and suspend execution until resumed } } let stream = fibonacciNumbers(); // create a lazy evaluated stream of numbers let first10 = Array.from(new Array(10), () => stream.next().value); // evaluate only the first 10 numbers console.log(first10); // the output is [0n, 1n, 1n, 2n, 3n, 5n, 8n, 13n, 21n, 34n] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)