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
12
votes
1 answer

Python asyncio, futures and yield from

Consider the following program (running on CPython 3.4.0b1): import math import asyncio from asyncio import coroutine @coroutine def fast_sqrt(x): future = asyncio.Future() if x >= 0: future.set_result(math.sqrt(x)) else: …
oberstet
  • 21,353
  • 10
  • 64
  • 97
11
votes
3 answers

Ruby on rails: Yielding specific views in a specific places in the layout

If I have one <%= yield %> tag then all my views render in the same place in the layout. Can I have different <%= yield %> tags for different views? Is so how do I do this? Thanks
user852974
  • 2,242
  • 10
  • 41
  • 65
11
votes
2 answers

Why is this python generator returning the same value everytime?

I have this generator that yields lists: def gen(): state = [None] for i in range(5): state[0] = i yield state And here's the output, when I call it: >>> list(gen()) [[4], [4], [4], [4], [4]] Why are all the elements [4]?…
piggs_boson
  • 987
  • 2
  • 12
  • 25
11
votes
3 answers

Custom Collection Implementing IEnumerable

I know that technically, an Interface is used for reading and not writting or editing however, I want to add an add and addrange function to the following class, here is what I currently have which is not working public class HrefCollection :…
Pierluc SS
  • 3,138
  • 7
  • 31
  • 44
11
votes
2 answers

Is it possible to yield two things at a time just like return?

def foo(choice): for i in limit: d1 = doSomeCalc() d2 = doSomeOtherCalc() if choice == "stuff": yield { d1 : "value" } else: yield { d2 :…
Animesh Pandey
  • 5,900
  • 13
  • 64
  • 130
11
votes
3 answers

Issue with a python function returning a generator or a normal object

I defined the function f as def f(flag): n = 10 if flag: for i in range(n): yield i else: return range(n) But f returns a generator no matter what flag is: >>> f(True)
Ziyuan
  • 4,215
  • 6
  • 48
  • 77
11
votes
2 answers

Rails: using "content_for" after the corresponding "yield" inside layout

I think this has been asked before but even though I searched Google I haven't come up with a solution. So this is what I'm trying to do in Rails 2.3.5: layouts/application.html.erb: ... some other stuff <%= yield :head %> …
thenoseman
  • 1,180
  • 1
  • 11
  • 12
11
votes
1 answer

Python - Understanding the send function of a generator

I'm studying in Python yield and find that yield is not only the way in which generators output a return value but also a way to put values into a generator. For example the following code def f(): print (yield), print 0, print (yield), …
neuront
  • 9,312
  • 5
  • 42
  • 71
10
votes
1 answer

Continuations and for comprehensions -- what's the incompatibility?

I am new to Scala and trying to wrap my head around continuations I'm trying to reproduce the yield return C# statement. Following this post, I have written the following code : package com.company.scalatest import…
GuiSim
  • 7,361
  • 6
  • 40
  • 50
10
votes
4 answers

What is better when using an IEnumerable with one item: yield return or return []?

This is one of those "you can do it many ways" questions. Consider the following code: protected virtual IEnumerable GetScriptReferences() { ScriptReference referece = new ScriptReference(); referece.Assembly =…
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
10
votes
3 answers

yield statement implementation

I want to know everything about the yield statement, in an easy to understand form. I have read about the yield statement and its ease when implementing the iterator pattern. However, most of it is very dry. I would like to get under the covers and…
Sasha
10
votes
2 answers

Behavior of sched_yield

I have few questions about the sched_yield function because I'm seeing that it is not functioning as intended in my code. Many times I see that the same thread runs again and again, even in the presence of other threads, when I try to yield it by…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
10
votes
4 answers

c# yield and try-finally

If I have a coroutine as follows, will the code in the finally block get called? public IEnumerator MyCoroutine(int input) { try { if(input > 10) { Console.WriteLine("Can't count that high."); yield break; } …
Liron
  • 2,012
  • 19
  • 39
10
votes
1 answer

Assigning (yield) to a variable

First of all, I'd like to mention that I am not particularly familiar with Python. I have recently been forced to familiarise myself with a code sample that's left my jaws ajar, and I have been unable to "translate" it. The various documents and…
Pyromonk
  • 684
  • 1
  • 12
  • 27
10
votes
8 answers

How to implement lazy sequence (iterable) in scala?

I want to implement a lazy iterator that yields the next element in each call, in a 3-level nested loop. Is there something similar in scala to this snippet of c#: foreach (int i in ...) { foreach (int j in ...) { …
duduamar
  • 3,816
  • 7
  • 35
  • 54