Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between yield and yield* in Dart

Tags:

flutter

dart

I wanted to know what's the difference between this two. I find this SO post on javascript, Delegated yield (yield star, yield *) in generator functions

From what I understand, yield* delegates to the another generator and after the another generator stop producing values, then it resumes generating its own values.

Explanation and examples on the dart side would be helpful.

like image 405
mirkancal Avatar asked Aug 14 '19 10:08

mirkancal


People also ask

What is Yield * In flutter?

Yield adds a value to the output stream of the surrounding async* function. It's like a return but doesn't terminate the function.

What is Sync * in Dart?

In Dart language the synchronous data sequence means the instance of Iterable . The asynchronous data sequence means the instance of Stream .

What's the difference between async and async * in Dart?

What is the difference between async and async* in Dart? The difference between both is that async* will always return a Stream and offer some syntax sugar to emit a value through the yield keyword. async gives you a Future and async* gives you a Stream.

What is a generator in Dart?

In Dart, a generator is a unique function that allows the user to produce a value sequence easily. Generators return values on demand when we try to iterate over them. Dart provides two types of generator functions that can be used to generate a sequence of values: Synchronous generator: Returns an iterable object.

What are sync* async* yield and yield* in Dart?

What are sync*, async*, yield and yield* in Dart? Ok, we have already seen async keyword lots of times before, but what is this async with a star next to it? Or sync*? To put it simply - Those are all keywords used in generator functions. Generator functions produce sequence of values (in contrast to regular functions that return single value).

What is the difference between “yield” and “return”?

It is generally used for the end of the execution and “returns” the result to the caller statement. It can return all type of values and it returns None when there is no expression with the statement “return”. Yield is generally used to convert a regular Python function into a generator.

What is the yield statement in Python?

The yield statement can be used only in the generator’s functions. The generator’s function generates data items in a natural way as calculated, received from outside, predefined values, etc. What does the Yield do?

What is a yield?

Yield is the income returned on an investment, such as the interest received from holding a security. The yield is usually expressed as an annual percentage rate based on the investment's cost, current market value , or face value.


1 Answers

yield

It is used to emit values from a generator either async or sync.

Example:

Stream<int> str(int n) async* {   for (var i = 1; i <= n; i++) {     await Future.delayed(Duration(seconds: 1));     yield i;   } }  void main() {   str(3).forEach(print); } 

Output:

1 2 3 

yield*

It delegates the call to another generator and after that generator stops producing the values, it resumes generating its own values.

Example:

Stream<int> str(int n) async* {   if (n > 0) {       await Future.delayed(Duration(seconds: 1));     yield n;     yield* str(n - 1);   } }  void main() {   str(3).forEach(print); } 

Output:

3 2  1  
like image 61
CopsOnRoad Avatar answered Sep 21 '22 02:09

CopsOnRoad