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
22
votes
1 answer

Understanding nested yield / return in python

I have a function in python whose output is a generator : def main_function(x): r = get_range() for i in range(r): yield x+i I want to refactor the code (I've simplified the use case but actual computation might be complex &…
user
  • 17,781
  • 20
  • 98
  • 124
22
votes
1 answer

What's the advantage of using yield in __iter__()?

What is the advantage of using an generator(yield) inside an __iter__() function? After reading through Python Cookbook I understand "If you want a generator to expose extra state to the user, don’t forget that you can easily implement it as a…
Joe_12345
  • 589
  • 2
  • 7
  • 19
22
votes
4 answers

Use of yield with a dict comprehension

As a contrived example: myset = set(['a', 'b', 'c', 'd']) mydict = {item: (yield ''.join([item, 's'])) for item in myset} and list(mydict) gives: ['as', 'cs', 'bs', 'ds', {'a': None, 'b': None, 'c': None, 'd': None}] What happens here? What does…
skyork
  • 7,113
  • 18
  • 63
  • 103
21
votes
7 answers

Behaviour of Python's "yield"

I'm reading about the yield keyword in python, and trying to understand running this sample: def countfrom(n): while True: print "before yield" yield n n += 1 print "after yield" for i in countfrom(10): print…
The Student
  • 27,520
  • 68
  • 161
  • 264
21
votes
3 answers

What is the preferred way to implement 'yield' in Scala?

I am doing writing code for PhD research and starting to use Scala. I often have to do text processing. I am used to Python, whose 'yield' statement is extremely useful for implementing complex iterators over large, often irregularly structured…
Urban Vagabond
  • 7,282
  • 3
  • 28
  • 31
21
votes
2 answers

How does Ruby on Rails use yield for layouts?

yield is used to call a block. How does this work in Rails where yield is used for layouts? -# application.html.haml %body= yield Does it use blocks somewhere or is the method simply overridden?
user142019
21
votes
3 answers

Unity - IEnumerator's yield return null

I'm currently trying to understand IEnumerator & Coroutine within the context of Unity and am not too confident on what the "yield return null" performs. At the moment i believe it basically pauses and waits for the next frame and in the next frame…
WonderfulWonder
  • 515
  • 1
  • 8
  • 20
21
votes
4 answers

what's the difference between yield from and yield in python 3.3.2+

After python 3.3.2+ python support a new syntax for create generator function yield from I have made a quick try for this by >>> def g(): ... yield from [1,2,3,4] ... >>> for i in g(): ... print(i) ... 1 2 3 4 >>> It seems…
Erxin
  • 1,786
  • 4
  • 19
  • 33
21
votes
5 answers

What is yield and what is the benefit to use yield in asp .NET?

Can you help me in understanding of yield keyword in asp .NET(C#).
Vijjendra
  • 24,223
  • 12
  • 60
  • 92
20
votes
1 answer

Making python generator via c++20 coroutines

Let's say I have this python code: def double_inputs(): while True: x = yield yield x * 2 gen = double_inputs() next(gen) print(gen.send(1)) It prints "2", just as expected. I can make a generator in c++20 like that: #include…
tort_dla_psa
  • 318
  • 1
  • 7
20
votes
6 answers

Meaning of the word yield

Currently, I'm reading "The Well-Grounded Rubyist" by David A. Black, and I'm stuck at chapter 10.9 (Enumerators and the next dimension of enumerability). My question is about the yield method. What is the meaning of the word yield in the Ruby…
kyrylo
  • 1,691
  • 1
  • 15
  • 22
20
votes
4 answers

Which is generally faster, a yield or an append?

I am currently in a personal learning project where I read in an XML database. I find myself writing functions that gather data and I'm not sure what would be a fast way to return them. Which is generally faster: yields, or several append()s…
Kit
  • 30,365
  • 39
  • 105
  • 149
20
votes
1 answer

Concurrency or Performance Benefits of yield return over returning a list

I was wondering if there is any concurrency (now or future), or performance benefit to using yield return over returning a list. See the following examples Processing Method void Page_Load() { foreach(var item in GetPostedItems()) …
bendewey
  • 39,709
  • 13
  • 100
  • 125
20
votes
4 answers

How to pass in an empty generator parameter?

I have a method which takes a generator plus some additional parameters and returns a new generator: function merge(\Generator $carry, array $additional) { foreach ( $carry as $item ) { yield $item; } foreach ( $additional as…
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56
20
votes
3 answers

Trying to understand generators / yield in node.js - what executes the asynchronous function?

Node.js now has generators. My understanding is that generators can be used to write code that appears to be much more linear and avoids callback hell and pyramid of doom style coding. So to this point, my understanding is that inside a generator,…
Duke Dougal
  • 24,359
  • 31
  • 91
  • 123