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

Scala : Better way to handle returning Future from Yeild to avoid future of future

Consider the below code snippet: In this, I am trying to get values from future using 'For - yield' comprehension. Now in yield method, I need to do a check which makes the call to a function fallbackResult which returns a future and hence return…
0
votes
3 answers

How do I make my code remember the current position and next time show the next element?

In Python there is iter() used like this: >>> a=[1,2,4] >>> b=iter(a) >>> b.next() 1 >>> b.next() 2 >>> b.next() 4 >>> b.next() Traceback (most recent call last): File "", line 1, in StopIteration >>> Does Ruby have the same…
mlzboy
  • 14,343
  • 23
  • 76
  • 97
0
votes
1 answer

yield, get rid of generator object, return one array instead of three

I have this code: import numpy as np class B(): def __init__(self,a,b): self.a = a self.b = b def __repr__(self): return 'B({0},{1})'.format(self.a,self.b) class test(): def __init__(self,name,method,…
George
  • 5,808
  • 15
  • 83
  • 160
0
votes
2 answers

Detecting if for-loop item is the last when yielding items?

I am processing a huge postgresql database for which I have created a "fetch" function. def fetch(cursor, batch_size=1e3): """An iterator that uses fetchmany to keep memory usage down""" while True: records =…
Zeliax
  • 4,987
  • 10
  • 51
  • 79
0
votes
1 answer

How can a generator be converted to a function that returns only the first value yielded by the generator?

I have some code that prints out any messages that it receives in a loop: import pytg import pytg.utils import pytg.receiver @pytg.utils.coroutine def receiver_function(tg_receiver): while True: message = (yield) …
d3pd
  • 7,935
  • 24
  • 76
  • 127
0
votes
1 answer

How to force a recursive function to yield skip frames?

How can I force a recursive function to yield frames at run time? I have tried to place yield functions at different stages of the recursive function. I don't understand what is happening, because the if condition to print the time every 1000…
bandybabboon
  • 2,210
  • 1
  • 23
  • 33
0
votes
1 answer

Blade template: Is it possible to yield a section which is defined in parent?

I'm trying to insert a piece of code wherever it is yielded, even if it is yielded inside a child view. I want the child view to load the section which is defined inside the parent. This should be a different position for every child. Is this…
Z0q
  • 1,689
  • 3
  • 28
  • 57
0
votes
2 answers

using yield print output

This is a continuation from here. I am using yield statement instead of return. This is the code: class Measurements(): def __init__(self, value, other): self.value = value self.other = other class Criteria(): def…
George
  • 5,808
  • 15
  • 83
  • 160
0
votes
2 answers

Laravel Blade not showing section

I'm having troubles with Blade showing a section. This is my main layout file: @include('layouts.header')
0
votes
3 answers

Abuse yield to avoid condition in loop

I need to search for the first, last, any, or all occurence of something in something else. To avoid repeating myself (DRY) I came up with the following solution. Of interest are the methods search_revisions() and collect_one_occurence() of both…
Lesmana
  • 25,663
  • 9
  • 82
  • 87
0
votes
1 answer

Return list from generator Python

I'm trying to make a customize version for DFS from the original version of networkx. You can relate to the original version of DFS from networkx here: networkx DFS In my implementation, I want to add a child which contain "if" in the label to a…
xtiger
  • 1,446
  • 2
  • 15
  • 33
0
votes
2 answers

Ruby, bubble_sort_by(arr), code is working but returning original array

Short question: Why my method (which is accepting blocks) is returning original array instead of modified. Long question: Code like this: def bubble_sort_by(array) array.each_with_index do |outer, outer_index| array.each_with_index do |inner,…
ToTenMilan
  • 582
  • 1
  • 9
  • 19
0
votes
1 answer

Unable to cast 'd__14`1[System.Object] to 'IEnumerable`1[System.Int32]'

I'm trying to create generator of IEnumerable to set School.Foundation, this is the Type that I'm trying to populate at the moment: public class School { public IEnumerable Foundation; } this is my generator so far: public static…
0
votes
1 answer

How to do coverage on a python coroutine?

I am using tornado coroutines in python 2.7 and I have done unit tests like this one: def test_my_coroutine_function(self): # Arranges ... # Acts response = yield my_function() # Asserts ... My function is defined like…
M07
  • 1,060
  • 1
  • 14
  • 23
0
votes
1 answer

Yield mutable.seq from mutable.traversable type in Scala

I have a variable underlying of type Option[mutable.Traversable[Field]] All I wanted todo in my class was provide a method to return this as Sequence in the following way: def toSeq: scala.collection.mutable.Seq[Field] = { for { f <-…
Chris Beck
  • 118
  • 7