In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I would like to talk to you about iterable objects and iterators in JavaScript Lazy evaluation. Many people may not know much about it. In order to make you understand better, the editor summarizes the following contents. I hope you can get something from this article.
Lazy evaluation
Lazy evaluation is often translated as "deferred evaluation" or "lazy evaluation", which means that the value of an expression is evaluated only when it really needs to be executed.
Contrary to lazy evaluation, early evaluation (eager evaluation), also known as greedy evaluation (greedy evaluation) or strict evaluation, is the evaluation strategy of most traditional programming languages.
The benefits of making full use of lazy evaluation are mainly reflected in the following two aspects:
Avoid unnecessary calculations and improve performance.
Save space and make the data structure of infinite loop possible.
Iterator
Iterators in ES6 make it possible to evaluate lazily and create user-defined data sequences. Iteration is a mechanism for traversing data. Iterators are pointers used to traverse data structure elements (called Iterable) and to produce a sequence of values.
An iterator is an object that can be iterated. It abstracts the data container so that it behaves like an iterable object.
The iterator does not evaluate the value of each item when it is instantiated and generates the next value only when requested. This is very useful, especially for large datasets or sequences of infinite elements.
Iterable object
An iterable object is a data structure that wants its elements to be accessible to the public. Many of the objects in JS are iterable, and they may not be well detected, but if you examine them carefully, you will find the characteristics of the iteration:
New Map ([iterable])
New WeakMap ([iterable])
New Set ([iterable])
New WeakSet ([iterable])
Promise.all ([iterable])
Promise.race ([iterable])
Array.from ([iterable])
There is also a need for an iterable object, otherwise it will throw a type error, such as:
For... Of
... (unfold operator) const [a, b,..] = iterable (deconstruction assignment)
Yield* (generator)
There are already many built-in iterators in JavaScript:
String,Array,TypedArray,Map,Set .
Iterative protocol
Iterators and iterable objects follow the iterative protocol.
A protocol is a set of interfaces and specifies how to use them.
The iterator follows the iterator protocol and can iteratively follow the iterable protocol.
Iterable protocol
To make an object iterable, it must implement an iterator method through Symbol.iterator, which is the factory of the iterator.
With TypeScript, the iterative protocol is as follows:
Interface Iterable {[Symbol.iterator] (): Iterator;}
Symbol.iterator] () is a function with no parameters. Calling it on an iterable object means that we can access the iterable object through this, which can be a regular function or a generator function.
Iterator protocol
The iterator protocol defines a standard method for generating sequences of values.
In order to make an object an iterator, it must implement the next () method. Iterators can implement the return () method, which we will discuss later in this article.
With TypeScript, the iterator protocol is as follows:
Interface Iterator {next (): IteratorResult; return? (value?: any): IteratorResult;}
IteratorResult is defined as follows:
Interface IteratorResult {value?: any; done: boolean;}
Done notifies the consumer whether the iterator has been used, false indicates that there are still values to be generated, and true indicates that the iterator is over.
Value can be any JS value, which is the value shown to the consumer.
When done is true, value can be omitted.
Combination
Iterators and iterable objects can be represented by the following figure:
Case study
After the introduction of the basics, let's cooperate with some examples to deepen our image.
Range iterator
Let's start with a very basic iterator, the createRangeIterator iterator.
We manually call it.next () to get the next IteratorResult. The last call returns {done:true}, which means that the iterator is now in use and no longer produces any values.
Function createRangeIterator (from, to) {let I = from Return {next () {if (I val > 1, map (/ / ⬆️ 5. Divide obtained values by 2 val = > val / 2, take (/ / ⬆️ 4. Take only six of them 6) Cycle (/ / ⬆️ 3. Make an infinite cycling sequence of them take (/ / ⬆️ 2. Take just three of them 3, evenNumbersIterator / / ⬆️ 1. Infinite sequence of even numbers)) Console.log (result): after reading the above, do you have any further understanding of iterable objects and iterators in JavaScript Lazy evaluation? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.