iterable, iterator, generator by gaurav khurana
TRANSCRIPT
![Page 1: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/1.jpg)
Iterable, Iterator, Generator
By Gaurav Khurana
![Page 2: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/2.jpg)
Iterator Object• Object is an Iterator object when it allows to access items from a
collection one at a time.• Provide next() method to achieve above.• next() method spits out object of this sort {value : ”something” , done : true/false}.
![Page 3: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/3.jpg)
Iterable interface• Object must provide default iteration method which returns an
iterator object.• [Symbol.iterator] is such method in javascript.• Its automatically gets called by for of loop.
![Page 4: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/4.jpg)
Symbol.iterator with other objects
![Page 5: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/5.jpg)
Some objects doesn’t have iterable Interface
![Page 6: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/6.jpg)
For of loop execute iterable interface
Log Result
![Page 7: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/7.jpg)
No iterable for Objects?
![Page 8: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/8.jpg)
But previous example restricts us to use keys as number lets solve that
![Page 9: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/9.jpg)
Quick Recap• The Iterable interface require the implementation of a
[Symbol.iterator] method.• The iterator interface require the implementation of next.• Why next()?
• Because next method’s result has a done property set to true means the iterator has been exhausted and the iteration value.
• Most Iterators implement Iterable interface i.e. most of them have [Symbol.iterator] method.
![Page 10: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/10.jpg)
Let’s simplify the confusing part
![Page 11: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/11.jpg)
Simplifying further, the need of it
![Page 12: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/12.jpg)
Array methods returns Iterable iterator
![Page 13: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/13.jpg)
Generators• Pause Run Pause.• Blocking the asynchronous function calls.• Implementing Iterables.
![Page 14: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/14.jpg)
Conventional function running
Result
![Page 15: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/15.jpg)
How generators are different?• Can be pause as many times and resumed later.• Allowing other code to run during these pause periods.• ES6 generator functions are "cooperative" in their concurrency
behavior which basically indicates that a process (in our case, a function) itself chooses when it will allow an interruption, so that it can cooperate with other code.• Use yield keyword to pause itself.
![Page 16: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/16.jpg)
Syntax please ?
![Page 17: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/17.jpg)
Generators produce iterables
![Page 18: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/18.jpg)
Communicating with Generators
![Page 19: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/19.jpg)
Where can we use them?
![Page 20: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/20.jpg)
Two way communication with Generator
![Page 21: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/21.jpg)
Array index using for of loop?
![Page 22: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/22.jpg)
Generators can help attaining array index using for of
![Page 23: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/23.jpg)
Error Handling
![Page 24: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/24.jpg)
Reverse Direction Error Handling
![Page 25: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/25.jpg)
Reverse error handling continue
![Page 26: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/26.jpg)
Delegating Generators with yield*
![Page 27: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/27.jpg)
Delegating and accepting returned value
![Page 28: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/28.jpg)
Generator can be used for async ajax
![Page 29: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/29.jpg)
Using generator for async
![Page 30: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/30.jpg)
![Page 31: Iterable, iterator, generator by gaurav khurana](https://reader035.vdocuments.mx/reader035/viewer/2022062522/58d093051a28abdd648b497d/html5/thumbnails/31.jpg)