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

yield always gets called

Currently I'm reading a collection of items from a stream. I do this as following: public class Parser{ private TextReader _reader; //Get set in Constructor private IEnumerable _items; public IEnumerable Items{ get{ //I…
Timo Willemsen
  • 8,717
  • 9
  • 51
  • 82
6
votes
2 answers

Scala List Comprehensions

I'm trying to generate a list in scala according to the formula: for n > 1 f(n) = 4*n^2 - 6*n + 6 and for n == 1 f(n) = 1 currently I have: def lGen(end: Int): List[Int] = { for { n <- List.range(3 , end + 1 , 2) } yields { 4*n*n - 6*n - 6…
LeeG
  • 265
  • 1
  • 4
  • 11
6
votes
2 answers

How to replace a yield with a map in Scala?

how can I remove this yield's? I wanna use a map instead of: val cols = for(x <- 0 to 6) yield for(y <- 0 to 5) yield apply(x, y) Is this possible? Thanks! Best regards, John
user1137701
  • 101
  • 3
6
votes
1 answer

Issue with Rails Content_for / yield - duplicated content

I'm trying to load some javascript in my application page using a named yield block, but the code is duplicated because of a generic yield that load my view pages. something like that: -----Code present in views----- <% content_for :bottom_scripts…
Guilherme
  • 61
  • 3
6
votes
1 answer

How to test an element from a generator without consuming it

I have a generator gen, with the following properties: it's quite expensive to make it yield (more expensive than creating the generator) the elements take up a fair amount of memory sometimes all of the __next__ calls will throw an exception, but…
Marses
  • 1,464
  • 3
  • 23
  • 40
6
votes
3 answers

How to run event loop when doing nested/recursive computations?

The usual examples of how to break a computation and release using setTimeout() seem to rely on having a shallow (1-deep) call stack. But what about when you are doing a deeply nested or mutually-recursive computation (like a tree search) and you…
Jack Punt
  • 342
  • 1
  • 14
6
votes
2 answers

Strange yield syntax in JavaScript

I just took a look at Dave Herman's very interesting task.js. In his example he has this line: var [foo, bar] = yield join(read("foo.json"), read("bar.json")).timeout(1000); I'm familiar with generators but I don't…
Matthew Gertner
  • 4,487
  • 2
  • 32
  • 54
6
votes
4 answers

Getting the value of a return after a yield in python

I would like to know how to get the return value of a function after all the execution of yield in a function like this: def gen_test(): l = [] for i in range(6): l.append(i) yield i # i want to know this value after all…
6
votes
4 answers

Python, redirecting the stream of Popen to a python function

I'm new to python programming. I have this problem: I have a list of text files (both compressed and not) and I need to : - connect to the server and open them - after the opening of the file, I need to take his content and pass it to another…
wheisenberg
  • 227
  • 3
  • 6
  • 15
6
votes
5 answers

What are the advantages of "yield item" vs return iter(items)?

In the examples below, resp.results is an iterator. Version1 : items = [] for result in resp.results: item = process(result) items.append(item) return iter(items) Version 2: for result in resp.results: yield process(result) Is…
espeed
  • 4,754
  • 2
  • 39
  • 51
6
votes
3 answers

Difference between yield statement in python and MyHDL

I am currently learning MyHDL for my summer project. I have a problem grasping the functioning of yield statement in it. Though its true that the MyHDL is based upon python, it uses its yield statement in a specialized way. the link for the same is…
Adwaitvedant
  • 253
  • 3
  • 10
6
votes
1 answer

`yield from` generator vs `yield from` list performance

Python 3.6.8 (default, Oct 7 2019, 12:59:55) Type 'copyright', 'credits' or 'license' for more information IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: def yield_from_generator(): ...: yield from (i for i in…
pt12lol
  • 2,332
  • 1
  • 22
  • 48
6
votes
3 answers

How do you use Ruby blocks to conditionally execute something?

I recently purchased the book Seven Languages in Seven Weeks and have been reading through the chapter on Ruby. In the section which introduces blocks (page 40), a code sample is given which illustrates the use of blocks for the purpose of…
Steve Johnson
  • 949
  • 3
  • 12
  • 13
6
votes
2 answers

Custom Keras Data Generator with yield

I am trying to create a custom data generator and don't know how integrate the yield function combined with an infinite loop inside the __getitem__ method. EDIT: After the answer I realized that the code I am using is a Sequence which doesn't need a…
oezguensi
  • 930
  • 1
  • 12
  • 23
6
votes
3 answers

Overloaded use of yield return

I don't have much experience with using the yield keyword. I have these IEnumerable extensions for Type Conversion. My question is does the first overloaded method have the same yield return effect that I'm getting from the second method? public…
bendewey
  • 39,709
  • 13
  • 100
  • 125