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
47
votes
11 answers

C++ Equivalent of C# Yield?

public void Consumer() { foreach(int i in Integers()) { Console.WriteLine(i.ToString()); } } public IEnumerable Integers() { yield return 1; yield return 2; yield return 4; yield return 8; yield return…
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
47
votes
3 answers

The idiomatic way to implement generators (yield) in Golang for recursive functions

[ Note: I read Python-style generators in Go, this is not a duplicate of it. ] In Python / Ruby / JavaScript / ECMAScript 6, generator functions could be written using the yield keyword provided by the language. In Go, it could be simulated using a…
46
votes
2 answers

Understanding Kotlin's yield function

I don't see a very clear definition of the yield function in Kotlin. Example in the link above doesn't mention much but the following, val sequence = sequence { val start = 0 // yielding a single value yield(start) // yielding an…
Ahmed
  • 2,966
  • 7
  • 42
  • 69
45
votes
2 answers

Is it safe to combine 'with' and 'yield' in python?

It's a common idiom in python to use context manager to automatically close files: with open('filename') as my_file: # do something with my_file # my_file gets automatically closed after exiting 'with' block Now I want to read contents of…
lesnik
  • 2,507
  • 2
  • 25
  • 24
45
votes
5 answers

How yield catches StopIteration exception?

Why in the example function terminates: def func(iterable): while True: val = next(iterable) yield val but if I take off yield statement function will raise StopIteration exception? EDIT: Sorry for misleading you guys. I know…
Sergey Ivanov
  • 3,719
  • 7
  • 34
  • 59
41
votes
3 answers

How to write Python generator function that never yields anything

I want to write a Python generator function that never actually yields anything. Basically it's a "do-nothing" drop-in that can be used by other code which expects to call a generator (but doesn't always need results from it). So far I have…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
40
votes
6 answers

What is the simplest way to create an empty iterable using yield in Python?

I was playing around with iterables and more specifically the yield operator in Python. While using test driven development to start writing a new iterable, I wondered what is the shortest code that could make this simple test for an iterable to…
Adam Lindberg
  • 16,447
  • 6
  • 65
  • 85
39
votes
6 answers

What's the use of yield break?

Possible Duplicate: What does “yield break;” do in C#? Can anyone see a use for the "yield break" statement that could not have been otherwise achieved by using "break" or "return". This statement seems to be utterly useless. What's more, without…
Miles Davis
39
votes
3 answers

Can I yield from an inner function?

With ES6 generators, I see code like this: var trivialGenerator = function *(array) { var i,item; for(var i=0; i < array.length; i++){ item = array[i]; yield item; }; }; Is it possible to write something more like the…
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
38
votes
1 answer

Are there better ways to prevent 'yield' when no block is passed in?

I have a method that yields, which looks like: def a_method(*params) # do something yield # do something else end I want this method to yield the block if a block is passed in; and if no block is passed in, the method should sliently skip the…
Tao
  • 970
  • 1
  • 12
  • 21
35
votes
8 answers

Some help understanding "yield"

In my everlasting quest to suck less I'm trying to understand the "yield" statement, but I keep encountering the same error. The body of [someMethod] cannot be an iterator block because 'System.Collections.Generic.List< AClass>' is not an…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
35
votes
2 answers

Thread.Sleep or Thread.Yield

I have a method that uses a background worker to poll a DLL for a status looking something like this: var timeout = DateTime.Now.AddSeconds(3); while (System.Status != Status.Complete // our status is not complete && DateTime.Now < timeout …
poco
  • 2,935
  • 6
  • 37
  • 54
34
votes
3 answers

Yield String from List[Char]

I have a l: List[Char] of characters which I want to concat and return as a String in one for loop. I tried this val x: String = for(i <- list) yield(i) leading to error: type mismatch; found : List[Char] required: String So how can I…
xyz
  • 341
  • 1
  • 3
  • 3
34
votes
3 answers

Is there any shorthand for 'yield all the output from a generator'?

Is there a one-line expression for: for thing in generator: yield thing I tried yield generator to no avail.
Walrus the Cat
  • 2,314
  • 5
  • 35
  • 64
34
votes
3 answers

what does yield as assignment do? myVar = (yield)

I'm familiar with yield to return a value thanks mostly to this question but what does yield do when it is on the right side of an assignment? @coroutine def protocol(target=None): while True: c = (yield) def coroutine(func): def…
Fire Crow
  • 7,499
  • 4
  • 36
  • 35