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.

1641 questions
11880
votes
47 answers

What does the "yield" keyword do?

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.
  • 134,175
  • 17
  • 51
  • 61
953
votes
19 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…
Herms
  • 35,385
  • 12
  • 76
  • 101
589
votes
25 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
  • 6,359
  • 4
  • 19
  • 33
556
votes
8 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?
skb
  • 29,114
  • 31
  • 93
  • 140
551
votes
9 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
  • 66,156
  • 10
  • 122
  • 144
331
votes
10 answers

What is Scala's yield?

I understand Ruby and Python's yield. What does Scala's yield do?
Geo
  • 88,754
  • 113
  • 327
  • 506
329
votes
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
  • 51,186
  • 19
  • 121
  • 157
277
votes
14 answers

What's the yield keyword in JavaScript?

I heard about a "yield" keyword in JavaScript, but I found very poor documentation about it. Can someone explain me (or recommend a site that explains) its usage and what it is used for?
mck89
  • 18,267
  • 15
  • 86
  • 103
187
votes
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…
Dewfy
  • 22,418
  • 12
  • 68
  • 116
186
votes
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,297
  • 18
  • 51
  • 77
171
votes
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,781
  • 20
  • 76
  • 138
167
votes
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,236
  • 3
  • 27
  • 24
137
votes
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,196
  • 3
  • 30
  • 44
114
votes
6 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
  • 210,086
  • 260
  • 609
  • 888
110
votes
9 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,527
  • 23
  • 52
  • 74
1
2 3
99 100