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

C# yield return performance

How much space is reserved to the underlying collection behind a method using yield return syntax WHEN I PERFORM a ToList() on it? There's a chance it will reallocate and thus decrease performance if compared to the standard approach where i create…
Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50
15
votes
5 answers

Using yield without return type

I have a big long looping procedure like this: public void Process() { bool done = false; do { //do stuff }while (!done); } that I'd like to chop into bits and have the calling routine display my progress in some sort of UI.…
Vincent Vancalbergh
  • 3,267
  • 2
  • 22
  • 25
15
votes
3 answers

Implementing yield in C

For example: int getNext(int n) { while (TRUE) { n = n+1; yield n; } } int main() { while (TRUE) { int n = getNext(1); if (n > 42) break; printf("%d\n",n); } } Such that the above…
Shmoopy
  • 5,334
  • 4
  • 36
  • 72
15
votes
7 answers

What are real life applications of yield?

I know what yield does, and I've seen a few examples, but I can't think of real life applications, have you used it to solve some specific problem? (Ideally some problem that cannot be solved some other way)
juan
  • 80,295
  • 52
  • 162
  • 195
14
votes
2 answers

In PHP: what is the difference between "return", "yield", "yield from" and mixing both yield and return in same function?

The difference between return and yield seemed clear until I figured out there was also yield from and the possibility to combine both return and yield in the very same function! My understanding of return was that everything after was not executed,…
user12877622
14
votes
6 answers

Is there a way to efficiently yield every file in a directory containing millions of files?

I'm aware of os.listdir, but as far as I can gather, that gets all the filenames in a directory into memory, and then returns the list. What I want, is a way to yield a filename, work on it, and then yield the next one, without reading them all into…
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
14
votes
5 answers

yield from a list of generators created from an array

I've got this recursive generator var obj = [1,2,3,[4,5,[6,7,8],9],10] function *flat(x) { if (Array.isArray(x)) for (let y of x) yield *flat(y) else yield 'foo' +…
georg
  • 211,518
  • 52
  • 313
  • 390
14
votes
4 answers

ES6 generators mechanism - first value passed to next() goes where?

When passing parameters to next() of ES6 generators, why is the first value ignored? More concretely, why does the output of this say x = 44 instead of x = 43: function* foo() { let i = 0; var x = 1 + (yield "foo" + (++i)); …
NeuronQ
  • 7,527
  • 9
  • 42
  • 60
14
votes
2 answers

Python yield vs Ruby yield

In Ruby, the yield keyword is used to yield to closures for blocks of execution. How does this keyword differ in the Python language?
nobody
  • 7,803
  • 11
  • 56
  • 91
14
votes
3 answers

Ruby check if block is nil

I call a method with a block; method do "Hello" end and the method is defined as; def method yield end and when defining method; i want to check if given block is empty (nil) or not, because the variable in the method may end up like…
tozlu
  • 4,667
  • 3
  • 30
  • 44
14
votes
4 answers

Is it possible to reasonably emulate yield-syntax, perhaps with help of Java 8?

I was experimenting with this question today, from Euler Problems: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the…
skiwi
  • 66,971
  • 31
  • 131
  • 216
14
votes
3 answers

Interesting use of the C# yield keyword in Nerd Dinner tutorial

Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this snippet of code: public IEnumerable GetRuleViolations() { if (String.IsNullOrEmpty(Title)) yield return new RuleViolation("Title…
devuxer
  • 41,681
  • 47
  • 180
  • 292
14
votes
2 answers

Yield only if pattern match

I am building a list of different case class objects based on a loop and a pattern match. I want to exclude (skip) the items hitting the default case (essentially filtering the list, and mapping to types in one step). I'm probably too new to Scala…
Joernsn
  • 2,319
  • 4
  • 25
  • 33
13
votes
6 answers

How is it possible to see C# code after compilation/optimization?

I was reading about the yield keyword when I came across a sample chapter from C# in Depth: http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx. The first block of code utilizes the yield keyword to make a simple iterator.…
Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
13
votes
2 answers

PHP - json_encode a generator object (using yield)

I have a very large array in PHP (5.6), generated dynamically, which I want to convert to JSON. The problem is that the array is too large that it doesn't fit in memory - I get a fatal error when I try to process it (exhausted memory). So I figured…
Iván Pérez
  • 2,278
  • 1
  • 24
  • 49