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
27
votes
2 answers

Implementing yield (yield return) using Scala continuations

How might one implement C# yield return using Scala continuations? I'd like to be able to write Scala Iterators in the same style. A stab is in the comments on this Scala news post, but it doesn't work (tried using the Scala 2.8.0 beta). Answers in…
Yang
  • 16,037
  • 15
  • 100
  • 142
27
votes
4 answers

implementing a state machine using the "yield" keyword

Is it feasible to use the yield keyword to implement a simple state machine as shown here. To me it looks like the C# compiler has done the hard work for you as it internally implements a state machine to make the yield statement work. Can you…
Matt Warren
  • 10,279
  • 7
  • 48
  • 63
26
votes
5 answers

JS: how to use generator and yield in a callback

I use JS generator to yield a value in a callback of setTimeout: function* sleep() { // Using yield here is OK // yield 5; setTimeout(function() { // Using yield here will throw error yield 5; }, 5000); } // sync const sleepTime =…
Xaree Lee
  • 3,188
  • 3
  • 34
  • 55
26
votes
3 answers

Loop over two generator together

I have two generators say A() and B(). I want to iterate over both the generators together. Something like: for a,b in A(),B(): # I know this is wrong #do processing on a and b One way is to store the results of both the functions in lists…
Abhishek Gupta
  • 6,465
  • 10
  • 50
  • 82
25
votes
3 answers

How two consecutive yield statement work in python?

I stumble upon this code from pymotw.com in merging and splitting section. from itertools import * def make_iterables_to_chain(): yield [1, 2, 3] yield ['a', 'b', 'c'] for i in chain.from_iterable(make_iterables_to_chain()): …
unnobtainium
  • 528
  • 1
  • 5
  • 15
25
votes
2 answers

Python's PEP 484 type annotation for Generator Expression

What is the correct type annotation for a function that returns a generator expression? e.g.: def foo(): return (x*x for x in range(10)) I can't figure out if this is -> Iterator[int], -> Iterable[int], -> Generator[int, None, None], or…
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
25
votes
3 answers

yield return versus return select

Which are the advantages/drawbacks of both approaches? return items.Select(item => DoSomething(item)); versus foreach(var item in items) { yield return DoSomething(item); } EDIT As they are MSIL roughly equivalent, which one you find more…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
24
votes
4 answers

Python generator to yield everything from another generator call

I have a Python generator that can call itself to get more elements to yield. It looks like this: def gen(list): # ... if list: for x in gen(list[1:]): yield x My question is about the last two lines: is there a more…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
24
votes
3 answers

Why do Python yield statements form a closure?

I have two functions that return a list of functions. The functions take in a number x and add i to it. i is an integer increasing from 0-9. def test_without_closure(): return [lambda x: x+i for i in range(10)] def test_with_yield(): for…
Alex
  • 1,432
  • 14
  • 26
24
votes
1 answer

C# yield in nested method

If I step through the following code the call to ReturnOne() is skipped. static IEnumerable OneThroughFive() { ReturnOne(); yield return 2; yield return 3; yield return 4; yield return 5; } static IEnumerator
David
  • 1,052
  • 1
  • 8
  • 19
24
votes
1 answer

Difference between "yield" of Tornado and "yield from" of asyncio in mechanism?

In Tornado, we usually write the following code to call a function asynchronously: class MainHandler(tornado.web.RequestHandler): @tornado.gen.coroutine def post(self): ... yield self.handleRequest(foo) ... …
Star Brilliant
  • 2,916
  • 24
  • 32
24
votes
7 answers

Does Scala have an equivalent to C# yield?

I'm new to Scala, and from what I understand yield in Scala is not like yield in C#, it is more like select. Does Scala have something similar to C#'s yield? C#'s yield is great because it makes writing iterators very easy. Update: here's a pseudo…
waterlooalex
  • 13,642
  • 16
  • 78
  • 99
23
votes
3 answers

Javascript Generators: Understanding them

I'm pretty sure my understanding of generators is inherently broken. All online resources seem to conflict and it makes for an incredibly difficult and confusing learning experience. From what I understand, the yield keyword enables a currently…
AdrianCooney
  • 727
  • 5
  • 13
23
votes
2 answers

How does this class implement the "__iter__" method without implementing "next"?

I have the following code in django.template: class Template(object): def __init__(self, template_string, origin=None, name=''): try: template_string = smart_unicode(template_string) except…
zjm1126
  • 63,397
  • 81
  • 173
  • 221
23
votes
7 answers

Caching IEnumerable

public IEnumerable ListModules() { foreach (XElement m in Source.Descendants("Module")) { yield return new ModuleData(m.Element("ModuleID").Value); } } Initially the above code is great since there is no need to…
djskinner
  • 8,035
  • 4
  • 49
  • 72