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
13
votes
3 answers

How does this function with a "yield" work in detail?

I got this method (inside a Unity C# Script), but I do not understand how the "yield" part actually works. I know from the MSDN that the function will return an IEnumerator which I could iterate throught, but this code waits 1,5 seconds and does not…
Julius F
  • 3,434
  • 4
  • 29
  • 44
13
votes
3 answers

Is it possible to implement Python yield functionality in freestanding C?

I recently came accross the yield keyword in Python (as well as JavaScript) - I understand that this is primarliy used for the generator pattern, but the language construct seems to be used in asynchronous functions as well where my interests lie.…
tinkerbeast
  • 1,707
  • 1
  • 20
  • 42
13
votes
6 answers

Iterator pattern in VB.NET (C# would use yield!)

How do implement the iterator pattern in VB.NET, which does not have the yield keyword?
c00ke
  • 2,245
  • 5
  • 26
  • 34
13
votes
7 answers

Multiple generators in a single loop within PHP

I need to write a simple script that loads data from multiple files and merges it somehow. However, given the fact that the files might be quite huge I'd like to load data partially. To do so I decided to use yield. And according to examples I found…
Tomasz Kapłoński
  • 1,320
  • 4
  • 24
  • 49
13
votes
3 answers

Dir.glob to get all csv and xls files in folder

folder_to_analyze = ARGV.first folder_path = File.join(Dir.pwd, folder_to_analyze) unless File.directory?(folder_path) puts "Error: #{folder_path} no es un folder valido." exit end def get_csv_file_paths(path) files = [] Dir.glob(path +…
sergserg
  • 21,716
  • 41
  • 129
  • 182
13
votes
3 answers

Difference between yield self and yield?

Could anyone please help me to understand the difference between "yield self" and "yield"? class YieldFirstLast attr_accessor :first, :last def initialize(first = nil, last = nil) @first = first @last = last yield…
vivek kumar
  • 179
  • 3
  • 8
13
votes
3 answers

Can yield produce multiple consecutive generators?

Here are two functions that split iterable items to sub-lists. I believe that this type of task is programmed many times. I use them to parse log files that consist of repr lines like ('result', 'case', 123, 4.56) and ('dump', ..) and so on. I would…
h2kyeong
  • 447
  • 3
  • 13
12
votes
6 answers

Using for...else in Python generators

I'm a big fan of Python's for...else syntax - it's surprising how often it's applicable, and how effectively it can simplify code. However, I've not figured out a nice way to use it in a generator, for example: def iterate(i): for value in i: …
James Brady
  • 27,032
  • 8
  • 51
  • 59
12
votes
2 answers

Laravel Blade: pass array as parameter to yield section

i would like to pass an array as parameter from my controller to the blade template. My controller looks like this: $myArray = array('data' => 'data'); return View::make('myTableIndex') ->nest('myTable', 'my_table_template', $myArray) In my…
Pascal Cloverfield
  • 561
  • 1
  • 6
  • 20
12
votes
5 answers

invoking yield for a generator in another function

suppose I have some manager object. This object's API has a main_hook function, that gets another function f as it's argument, and runs the given f in a loop, doing some stuff in between each iteration: def main_hook(self,f): while…
olamundo
  • 23,991
  • 34
  • 108
  • 149
12
votes
4 answers

When is 'Yield' really needed?

Possible Duplicate: C# - Proper Use of yield return What can be a real use case for C# yield? Thanks.
user360455
12
votes
5 answers

using yield in C# like I would in Ruby

Besides just using yield for iterators in Ruby, I also use it to pass control briefly back to the caller before resuming control in the called method. What I want to do in C# is similar. In a test class, I want to get a connection instance, create…
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
12
votes
1 answer

Difference between `yield from foo()` and `for x in foo(): yield x`

In Python most examples of yield from explain it with saying that yield from foo() is similar to for x in foo(): yield x On the other hand it doesn't seem to be exactly the same and there's some magic thrown in. I feel a bit uneasy about using a…
Christian
  • 25,249
  • 40
  • 134
  • 225
12
votes
2 answers

Enumerating over lambdas does not bind the scope correctly?

consider the following C# program: using System; using System.Linq; using System.Collections.Generic; public class Test { static IEnumerable Get() { for (int i = 0; i < 2; i++) { int capture = i; …
Imi
  • 1,579
  • 1
  • 12
  • 22
12
votes
4 answers

PHP - How to count a generators yields

Using PHP >= 5.5 if we have a method that yielded values, what would be the best method in counting these values? What I was expecting was to be able to convert a Generator to an array and count that, however it would return an empty array. Count()…
user2899943