Questions tagged [yield]

yield is (1) a keyword that facilitates creation of generator functions, (2) a Ruby statement to transfer control from one coroutine to another, (3) a Java statement used to yield a value from a switch expression.

In , the yield statement is only used when defining a generator function, and is only used in the body of the generator function. Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function.

In , the yield statement is in context of coroutines generally used to transfer control from one coroutine to another, for example from a method to a block passed into it as an argument.

’s yield return is equivalent to Python’s yield, and yield break is just return in Python. In C#, yield is used in an iterator block to provide a value to the enumerator object or to signal the end of iteration.

yield is used in and generator functions in the same way it is in Python generator functions.

In yield is used in for-comprehension construction. for-comprehension iterates over one or more collections and uses yield to create and return new collection.

In , yield is a keyword used in a statement yield: <yield-target> to yield a value, which becomes the value of the enclosing switch expression.

1730 questions
50 answers

What does the "yield" keyword do in Python?

What is the use of the yield keyword in Python? What does it do? For example, I'm trying to understand this code1: def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: …
Alex. S.
  • 143,260
  • 19
  • 55
  • 62
20 answers

What is the yield keyword used for in C#?

In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet: IEnumerable FilteredList() { foreach(object item in FullList) { if(IsItemInPartialList(item)) yield…
  • 37,540
  • 12
  • 78
  • 101
27 answers

How can I wait In Node.js (JavaScript)? l need to pause for a period of time

I'm developing a console script for personal needs. I need to be able to pause for an extended amount of time, but, from my research, Node.js has no way to stop as required. It’s getting hard to read users’ information after a period of time... I’ve…
Christopher Allen
  • 7,769
  • 5
  • 20
  • 35
11 answers

In practice, what are the main uses for the "yield from" syntax in Python 3.3?

I'm having a hard time wrapping my brain around PEP 380. What are the situations where yield from is useful? What is the classic use case? Why is it compared to micro-threads? So far I have used generators, but never really used coroutines…
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
9 answers

What does "yield break;" do in C#?

I have seen this syntax in MSDN: yield break, but I don't know what it does. Does anyone know?
  • 30,624
  • 33
  • 94
  • 146
11 answers

What is Scala's yield?

I understand Ruby and Python's yield. What does Scala's yield do?
  • 93,257
  • 117
  • 344
  • 520
8 answers

IEnumerable and Recursion using yield return

I have an IEnumerable method that I'm using to find controls in a WebForms page. The method is recursive and I'm having some problems returning the type I want when the yield return is returnig the value of the recursive call. My code looks as…
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
15 answers

What's the yield keyword in JavaScript?

I heard about a "yield" keyword in JavaScript. What is it used for and how do I use it?
  • 18,918
  • 16
  • 89
  • 106
18 answers

Resetting generator object in Python

I have a generator object returned by multiple yield. Preparation to call this generator is rather time-consuming operation. That is why I want to reuse the generator several times. y = FunctionWithYield() for x in y: print(x) #here must be…
  • 23,277
  • 13
  • 73
  • 121
6 answers

Return all enumerables with yield return at once; without looping through

I have the following function to get validation errors for a card. My question relates to dealing with GetErrors. Both methods have the same return type IEnumerable. private static IEnumerable GetErrors(Card card) { var…
John Oxley
  • 14,698
  • 18
  • 53
  • 78
8 answers

Why use the yield keyword, when I could just use an ordinary IEnumerable?

Given this code: IEnumerable FilteredList() { foreach( object item in FullList ) { if( IsItemInPartialList( item ) ) yield return item; } } Why should I not just code it this way?: IEnumerable
James P. Wright
  • 8,991
  • 23
  • 79
  • 142
11 answers

When NOT to use yield (return)

This question already has an answer here: Is there ever a reason to not use 'yield return' when returning an IEnumerable? There are several useful questions here on SO about the benefits of yield return. For example, Can someone demystify the…
Lawrence P. Kelley
  • 4,276
  • 3
  • 28
  • 24
13 answers

Equivalent C++ to Python generator pattern

I've got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some…
Noah Watkins
  • 5,446
  • 3
  • 34
  • 47
3 answers

What is the return type hint of a generator function?

I'm trying to write a :rtype: type hint for a generator function. What is the type it returns? For example, say I have this functions which yields strings: def read_text_file(fn): """ Yields the lines of the text file one by one. :param…
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
4 answers

Why can't yield return appear inside a try block with a catch?

The following is okay: try { Console.WriteLine("Before"); yield return 1; Console.WriteLine("After"); } finally { Console.WriteLine("Done"); } The finally block runs when the whole thing has finished executing (IEnumerator
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
2 3
99 100