#21 - Dart Synchronous Workflows, Iterables, sync* generator functions, yield, yield*

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on everyone i'm wicked welcome back to my channel in the previous part i showed you a comprehensive explanation on how isolates work inside art while also tackling some of the fundamental differences between synchronous and asynchronous events today it's time to get into more details about the synchronous part of the equation therefore make sure you stick to this video as some of the facts i'm going to talk about will most likely surprise you so without further introduction let's get right into this tutorial okay so let's start by defining what asynchronous operation is a synchronous operation is more like a task a task that you definitely need to solve between moving on to the next one take a car wash for example there are a lot of synchronous operations you can observe right there first and foremost the guy pre-washes the car then covers it in active foam the resulting dirt and debris washes away with a pressure washer then wax will be applied on the car the entire car will then be rinsed one more time before going into the drying section all of these tasks from the timeline need to be taken care of synchronously you can't do both pre-washing and covering the car in foam since well the foam will be constantly washed away by the jet stream you need to tackle every task one at a time and move on to the next one but only after you finish the current one thus resulting in asynchronous workflow the art uses synchronous operations a large majority of time we saw in the previous tutorial how dart reads a program synchronously one line after another issuing the corresponding events in any dark package we will code you'll notice that the tendency is to have a problem that needs to be solved therefore you'll split it into multiple smaller problems like these little containers so now in order to solve the entire problem you'll need to solve those little problems one by one of course there is a defined order in which you should tackle them highlighted by the order in which you're calling the functions into the code these problems will most of the time return something as a result of their work otherwise they're not really that useful if they're not aiding towards solving the bigger problem right in asynchronous operation task or function you're used to it returning a value a type a class it may calculate an integer a string or perhaps it may return a student class we're talking of returning a single value in this case but what if we want the result of a synchronous operation to consist of 0 1 or multiple values can the event loop process asynchronous task and then return multiple values out of it you might be tempted to say well it can return a list of those objects what's the big deal about it well it's not always that simple the truth is it will return an iterable collection of those values types or classes so what's exactly the difference between a list or an iterable you might ask well an iterable is a much more abstract collection it is lazily constructed meaning that it will only generate its items whenever you access one of them an issuable collection is being traversed with the help of an iterator which is just a helper containing the current element it's at and also a function that will let it advance to the next element therefore an interval doesn't need to have a specified length it's infinite as in order to access an element all of the elements are always regenerated with an iterator until it finds the one you're looking for a list on the other hand is a special non-lazy type of iterable it is constructed directly the moment you call or declare it it always has a defined size it doesn't need to have an iterator since all values have their own index and you can access them directly by using the list access operator this is why when looping through an interval you can't use an ordinary for statement as you cannot access the elements of it directly you'll have to use the for in statement or iterable dot for each method to achieve that one important thing to be noted though is that since this iterable is such an abstract type it can be used to incorporate any number of generated results and you might imagine this is exactly what we need for generating multiple values synchronously we don't need the restrictions of a list we just need something that's more unconstrained this is why if you want to synchronously calculate a single element you'll have a type here and if you want to calculate multiple elements you'll have to return an iterable collection of those types as a quick preview maybe you can make an easier analogy in an asynchronous workflow returning a single value results in a future of this type and returning multiple values results in a stream of these types these are their correspondence when talking asynchrony we'll get to those in part 3 of this chapter but for now let's focus on our synchronous characteristics also if you find this tutorial really useful and you appreciate the way i teach these concepts please make sure to share the video hit that like and subscribe button and consider hitting the notification bell so that you'll know when i post a new video also make sure to follow me on twitter at let's get wicked this is where i'm at most of the time having that said let's get back into our tutorial let's start easy with something you definitely have seen before we have a simple function returning the sum of two elements a and b this is actually the case that corresponds to synchronously generating a single value out of this function this kind of function will always return a value right well but what if we want to have a function that can generate none one or multiple values well in this case we can really make use of the iterable class what we're going to do here is specify dart i don't want to have all values in a collection returned as a wall i want to generate them one by one synchronously when they're needed say for example we have a function called show that will take an n parameter and generate all elements from 1 to n this is all we want well in this case we need to mark this function as being a synchronous generator by using the sync star keyword in asynchronous generator you can't simply use return as that would literally return a single unit type whether it's an integer or a list of integer every time returns terminates the function and returns only one value instead you'll have to use yield so that you can generate values out of the function imagine a synchronous generator function having in mind what we saw in the previous tutorial dark processes the lines of code in its internal synchronous generator function and then yields the events one at a time it does that synchronously so it yields an event only after the current line of code has been traversed then it moves on to the second one checks it then yields another event and so on and so forth you might imagine why you can even generate no elements from a synchronous generator function the ill statement might never be called it is a possibility a normal function on the other hand will always have to return a value no matter what but now i am sure you still have a couple of questions regarding why we should use a generator function instead of a normal function returning a list of elements this is exactly why we're going to create a normal function having the same functionality as the generator function we'll call it show normal as it's a normal function compared to the show generated one we created earlier which is a generator function notice i have placed some print statements that will help us better debug and understand what's happening with our program now let's say we call the show normal function with a number of 10 and assign it to a variable what do you think is going to happen if i run this program right now well it will print that it started the job print the i values one by one from inside the loop and that it ended the job now what do you think will happen if we call the show generated function again with the same number of 10 the same thing right well surprise nothing happened why because as i told you an interval is a lazy collection meaning that it will only be generated when a potential element from inside of it gets accessed inside the code so let's see what happens if we access something like the last element of the generated collection now as you can notice the generator function will run and start generating the elements up until the last one then return it if we try the same thing in the case of the list you might say that it prints mostly the same exact thing although it looks the same it's not the same behavior underneath you'll understand the major difference up next what do you think is going to happen if we print the first element of the list now well it will print the first element right after it printed the last one and that's because the collection is already there every item inside of it has its own predefined space and can be accessed directly in the case of generators and iterables this is not the same when calling both last and first elements of an iterable collection generated by our function notice one really important key difference it runs the generator once in order to access the last element but it is incredibly clever that for the first element the generator only has to run for one single position instead of generating the entire iterable again then accessing the first element so having this in mind we can observe two major differences between lists and iterables intervals are lazy loaded meaning that the generator function will run only when a potential generated element from inside of it is accessed and the second one is that iterables generate just the right amount of elements it needs we saw that we don't need the entire amount of generated items to access the first one either way if you want to store all elements generated by asynchronous generator function into a list you can do it easily by calling the to list method but this is what i wanted to highlight you should always use it trebles when working with synchronous generator functions as they're smart and efficient and not only here notice that iterables are all over the place for example if you have a list and you want to retrieve its even elements the result of this is going to be a lazy iterable that will generate its values only when a potential item from inside of it is accessed you can even create an instance of an iterable directly by using the generate constructor for example again this is set to generate three integer values from one to three it won't generate them right away but only when they're accessed and if for example we want to access the element at position one or two the generator will run only until the one or 2 value is accessed it will continue with generating the value 3 since it's not needed in the program also i hope you realize that everything i said happens synchronously every item is generated one after another and no other task can run in between this process please do not confuse laziness with asynchrony asynchrony means hey i know i have to generate these values i will generate them in the background you can do other things in between and laziness means hey i know what i have to generate i'll just generate them synchronously in this case when you need a specific value from them one quick tip i want to give you before we end this tutorial is that inside a generator function you can call another generated function by using the yield star keyword for example along with those numbers here we'd also want to generate their negative siblings that are being calculated in a separate generator well all you have to do is to use the ill star keyword in order to call that generator function inside our initial generator function and voila now if you need the last element both generators will run and having this said i think it's finally time to end part 2 of this dart sync and async chapter i hope you understood how to create use and consume a generator function and especially the difference between iterables and lists in the next part i'm going to dive into every characteristic of asynchronous workflows discussing and displaying everything you need to know about asynchronously playing with one or more values we're gonna talk about futures and streams so prepare yourself as it is going to be a really important lesson to be understood as always if you like this tutorial don't forget to smash that like button subscribe to my channel and share the video with all of your friends and colleagues in pursuit of top tier development until next time as always take care wicked is out bye bye
Info
Channel: Flutterly
Views: 1,290
Rating: undefined out of 5
Keywords: dart, darttutorial, dart course, dart sync, dart synchronous, dart synchronous workflows, dart sync*, dart synchronous generator functions, dart generator functions, dart yield keyword, dart yield* keyword
Id: 60UdftkluF8
Channel Id: undefined
Length: 14min 4sec (844 seconds)
Published: Mon Sep 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.