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
8
votes
4 answers

What does yield do in python 2.7?

Possible Duplicate: The Python yield keyword explained Okay, I've probably phrased the question badly but this is the situation I have. I have this line of code in Python 2.7 which I'm trying to understand: yield (padding_zeros +…
GeorgePotter
  • 889
  • 1
  • 10
  • 18
8
votes
1 answer

python: yield inside map function

Is it possible to use yield inside the map function? For POC purpose, I have created a sample snippet. # Python 3 (Win10) from concurrent.futures import ThreadPoolExecutor import os def read_sample(sample): with open(os.path.join('samples',…
Jay Joshi
  • 1,402
  • 1
  • 13
  • 32
8
votes
7 answers

How does method yield work?

In javadoc there is said that yield method Causes the currently executing thread object to temporarily pause and allow other threads to execute. And Katherine Sierra and Bert Bates SCJP book says that yield() is supposed to do is make the…
Shikarn-O
  • 3,337
  • 7
  • 26
  • 27
8
votes
4 answers

Trouble yielding inside a block/lambda

I have the following Ruby code: # func1 generates a sequence of items derived from x # func2 does something with the items generated by func1 def test(x, func1, func2) func1.call(x) do | y | func2.call(y) end end func1 = lambda do |…
AJM
  • 655
  • 1
  • 9
  • 19
8
votes
1 answer

Tensorflow Dataset.from_generator fails with pyfunc exception

I am trying tensorflow's nightly 1.4 as I need Dataset.from_generator to stich together some variable length datasets. This simple code (idea from here): import tensorflow as tf Dataset = tf.contrib.data.Dataset it2 =…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
8
votes
2 answers

python - what does yield (yield) do?

Since python 2.5 there is the ability to send(), throw(), close() into a generator. Inside the defined generator one can 'catch' the sent data by doing something like: def gen(): while True: x = (yield) if x == 3: …
Aaron_ab
  • 3,450
  • 3
  • 28
  • 42
8
votes
1 answer

yield return and return

I often find myself writing sth. like this: if (condition) { yield return whatever; yield break; } I find it quite verbose to have to use two yield statements for the standard paradigm "return one value and exit method". I know I can just…
VVS
  • 19,405
  • 5
  • 46
  • 65
8
votes
1 answer

How to wrap or embed generators?

I'm trying to provide a unified interface for retrieving all files from a single directory or a list of directories. def get_files(dir_or_dirs): def helper(indir): file_list = glob.glob("*.txt") for file in file_list: …
galactica
  • 1,753
  • 2
  • 26
  • 36
8
votes
3 answers

Raising exception in a generator, handle it elsewhere and vice versa in python

I'm thinking in a direction more advanced as well as difficult to find solutions this problem. Before coming to any decision, I thought of asking expert advice to address this problem. The enhanced generators have new methods .send() and .throw()…
user5143593
8
votes
3 answers

What is the best way to translate this recursive python method into Java?

In another question I was provided with a great answer involving generating certain sets for the Chinese Postman Problem. The answer provided was: def get_pairs(s): if not s: yield [] else: i = min(s) for j in s - set([i]): …
anon
8
votes
4 answers

Ruby equivalent of C#'s 'yield' keyword, or, creating sequences without preallocating memory

In C#, you could do something like this: public IEnumerable GetItems() { for (int i=0; i<10000000; i++) { yield return i; } } This returns an enumerable sequence of 10 million integers without ever allocating a collection in…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
8
votes
4 answers

Misunderstood python yield

This code below works correct : def file_gen(f_name): f = open(f_name) for line in f: yield line gen_line = file_gen("foo.html") gen_line.next() # '\n' gen_line.next() # ' \n' gen_line.next() # ... next line in…
A.M. Sultanov
  • 147
  • 2
  • 8
8
votes
2 answers

python generator of generators?

I wrote a class that reads a txt file. The file is composed of blocks of non-empty lines (let's call them "sections"), separated by an empty line: line1.1 line1.2 line1.3 line2.1 line2.2 My first implementation was to read the whole file and…
crusaderky
  • 2,552
  • 3
  • 20
  • 28
8
votes
3 answers

Fibers in C#: are they faster than iterators, and have people used them?

So I was chatting with a colleague about fibers and turned up this paper from 2003 that describes a implementation of coroutines in C# using the Fiber API. The implementation of Yield in this paper was for .NET 1.1, so it predates the yield return…
Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
8
votes
3 answers

EmberJS multiple yield helper

I have a custom view that I've created in Ember. I really love the {{yield}} helper to allow me to control the 'bread' of the sandwich. However, what I'd like to do now, is create a 'double decker' sandwich, and have a view with more than 1 yield…
Ben
  • 16,124
  • 22
  • 77
  • 122