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
116
votes
9 answers

Is there a Java equivalent to C#'s 'yield' keyword?

I know there is no direct equivalent in Java itself, but perhaps a third party? It is really convenient. Currently I'd like to implement an iterator that yields all nodes in a tree, which is about five lines of code with yield.
ripper234
  • 222,824
  • 274
  • 634
  • 905
112
votes
10 answers

What are the main uses of yield(), and how does it differ from join() and interrupt()?

I am a little bit confused about the use of Thread.yield() method in Java, specifically in the example code below. I've also read that yield() is 'used to prevent execution of a thread'. My questions are: I believe the code below result in the same…
divz
  • 7,847
  • 23
  • 55
  • 78
108
votes
4 answers

What does 'yield' keyword do in flutter?

What does the yield keyword actually do in Dart?
Newaj
  • 3,992
  • 4
  • 32
  • 50
105
votes
7 answers

Rails check if yield :area is defined in content_for

I want to do a conditional rendering at the layout level based on the actual template has defined content_for(:an__area), any idea how to get this done?
William Yeung
  • 10,368
  • 9
  • 36
  • 42
104
votes
3 answers

Recursion using yield

Is there any way to mix recursion and the yield statement? For instance, a infinite number generator (using recursion) would be something like: def infinity(start): yield start # recursion here ... >>> it = infinity(1) >>> next(it) 1 >>>…
juliomalegria
  • 24,229
  • 14
  • 73
  • 89
98
votes
4 answers

Return and yield in the same function

What exactly happens, when yield and return are used in the same function in Python, like this? def find_all(a_str, sub): start = 0 while True: start = a_str.find(sub, start) if start == -1: return yield start …
nekomimi
  • 1,310
  • 3
  • 10
  • 14
96
votes
8 answers

Python: generator expression vs. yield

In Python, is there any difference between creating a generator object through a generator expression versus using the yield statement? Using yield: def Generator(x, y): for i in xrange(x): for j in xrange(y): yield(i,…
cschol
  • 12,799
  • 11
  • 66
  • 80
94
votes
5 answers

In C#, why can't an anonymous method contain a yield statement?

I thought it would be nice to do something like this (with the lambda doing a yield return): public IList Find(Expression> expression) where T : class, new() { IList list = GetList(); var fun =…
Lance Fisher
  • 25,684
  • 22
  • 96
  • 122
87
votes
5 answers

for x in y(): how does this work?

I was looking for code to spin a cursor in the terminal and found this. I was wondering what was happening in the code. In particular for c in spinning_cursor(): I've never seen this syntax. Is it because I am returning one element from a generator…
Paul
  • 5,756
  • 6
  • 48
  • 78
84
votes
5 answers

Ruby: Proc#call vs yield

What are the behavioural differences between the following two implementations in Ruby of the thrice method? module WithYield def self.thrice 3.times { yield } # yield to the implicit block argument end end module WithProcCall def…
Sam Stokes
  • 14,617
  • 7
  • 36
  • 33
81
votes
4 answers

What does the new keyword "yield" mean in Java 13?

Java 13 introduced the yield keyword for switch expressions. How can I use it and what's the difference to a default return or break value?
zerocewl
  • 11,401
  • 6
  • 27
  • 53
80
votes
3 answers

How to use 'yield' inside async function?

I want to use generator yield and async functions. I read this topic, and wrote next code: import asyncio async def createGenerator(): mylist = range(3) for i in mylist: await asyncio.sleep(1) yield i*i async def start(): …
Ильдар
  • 963
  • 1
  • 7
  • 11
80
votes
1 answer

yield in list comprehensions and generator expressions

The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] at 0x0245C148> >>> list([(yield i) for i in range(3)]) [0, 1, 2] >>> list((yield i) for i in range(3)) [0, None,…
zabolekar
  • 1,624
  • 1
  • 12
  • 17
76
votes
7 answers

Converting "yield from" statement to Python 2.7 code

I had a code below in Python 3.2 and I wanted to run it in Python 2.7. I did convert it (have put the code of missing_elements in both versions) but I am not sure if that is the most efficient way to do it. Basically what happens if there are two…
vkaul11
  • 4,098
  • 12
  • 47
  • 79
75
votes
5 answers

How to yield results from a nested generator function?

I have a function which yields results as it downloads them. For the purposes of this question, lets say I yield a sting once every second but I want a convenience function to wrap my generator: import time def GeneratorFunction(max_val): for i…
Jon Cage
  • 36,366
  • 38
  • 137
  • 215