Connect a “listen to your words, such as listen to words, explain the explanation of” lazy evaluation “~”, have digging friends ask: “I understand the meaning of lazy evaluation, but in JS how to achieve thunk?”

JS, unlike Haskell, does not support lazy evaluation from a language design perspective, but it can be modeled using syntax.

Think about it. What JS syntax can we use to simulate this “deferred computing” feature?

If you have no idea, look at the previous sentence:

In the Haskell’s Guide to Fun, thunk is translated as assurance;

In Haskell’s Introduction to Functional Programming, thunk is explained as:

Thunk stands for Formal real replacement program (sometimes called Suspended Computation). Thunk refers to a procedure in which the parameters or results of a function are represented during computation. This is called a thunk. You can simply think of thunk as a function of an incomplete expression and the environment variables needed to evaluate the result of that expression, forming a parameterless closure with the environment variables. So thunk has all the information you need to find this expression, just not when you don’t need it.


Does that mean Promise?

Actually, no!

Once a Promise executes, it starts executing, you just know that it’s Pending, but you don’t know if it’s just starting, or it’s almost finished, or some other stage of execution; By the time a Promise is fetched, the internal asynchronous task is already started and execution cannot be cancelled (this is one of the drawbacks of promises :);

Code 🌰

Promise is evaluated immediately, not lazily!


What else do you have in your hand?

Isn’t “deferred execution” just “paused execution later”? Thunk is more like Generator!! 👏

When assigning, I do not calculate, wrap you as a

, wait until you call next(), I will calculate;

Code 🌰

Isn’t this the simplest version of the JS lazy evaluation Thunk implementation?

Isn’t an infinite list in Haskell just an infinite iterator implemented by Generator in MDN?

function* idMaker(){ let index = 0; while(true) yield index++; } let gen = idMaker(); // "Generator { }" console.log(gen.next().value); // 0 console.log(gen.next().value); // 1 console.log(gen.next().value); / / 2 / /...Copy the code

Lazy.js is actually made “Lazy” by the Generator!

Take implementing the take method as an example 🌰 :

In Haskell, the take function obtains several elements of a list consecutively from scratch;

Prelude> take 3 [1,2,3,4,5]
[1,2,3]
Copy the code

JS simulation implementation take:

function* take(n,items){ let i = 0; if (n < 1) return; for (let item of items) { yield item; i++; if (i >= n) { return; }}} let thunk = take (3, [1, 2, 3, 4, 5]) console. The log (thunk. The next ()) / / {value: 1, done: false} console.log(thunk.next()) // {value: 2, done: false} console.log(thunk.next()) // {value: 3, done: false} console.log(thunk.next()) // {value: undefined, done: true}Copy the code

Lazy.js is called like this:

Lazy(stream).take(5) // Just read the first five blocks of data.each (processData);Copy the code

summary

The introduction to the column quoted this:

To get an overall picture of a person’s core JavaScript skills, I’m most interested in how they use closures and how they take advantage of asynchrony. — Jake Archibald

Consider this wiki explanation of closures:

  • Closures are useful for:Because closures only perform operations when they are called(leaving aside the overhead of generating the closure object itself, such as value capture in C++ means executing the copy constructor), i.e.Inertia is evaluated”, so it can be used to define control structures. For example:SmalltalkAll control structures in the language, including branching conditions (if/then/else) and loops (while and for), are implemented through closures. Users can also define their own control structures using closures.

Now it seems that lazy evaluation can bridge the “How to use closures” and “How to make the most of asynchrony”!!

The idea of “inertia” goes deep into functional programming and, most importantly, Monad, which delays the processing of “side effects,” also echoes the idea of “inertia,” which will be discussed later

Well, that’s it

Dig text is not easy, click to encourage 👍👍👍

I am Anthony Nuggets, the public account of the same name, output exposure input, technical insights into life, goodbye ~