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

May a while loop be used with yield in scala

Here is the standard format for a for/yield in scala: notice it expects a collection - whose elements drive the iteration. for (blah <- blahs) yield someThingDependentOnBlah I have a situation where an indeterminate number of iterations will…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
17
votes
2 answers

Error 'Iterator cannot contain return statement ' when calling a method that returns using a yield

I'm hoping there's a nicer way to write this method & overloads with less code duplication. I want to return a sequence of deltas between items in a list. this method:- public static IEnumerable CalculateDeltas(this IEnumerable
Dave00Galloway
  • 609
  • 1
  • 6
  • 20
17
votes
2 answers

SyntaxError: Unexpected Identifier (Generators in ES6)

I came up with this simple experiment after reading the documentation on generators from MDN: var nodes = { type: 'root', value: [ { type: 'char', value: 'a' }, { type: 'char', value: 'b' }, { type: 'char', value: 'c'…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
17
votes
4 answers

Pitfalls of (Mis)Using C# Iterators to Implement Coroutines

I am writing refactoring a Silverlight program to consumes a portion of its existing business logic from a WCF service. In doing so, I've run into the restriction in Silverlight 3 that only allows asynchronous calls to WCF services to avoid cases…
LBushkin
  • 129,300
  • 32
  • 216
  • 265
16
votes
3 answers

Scala: Yielding from one type of collection to another

Concerning the yield command in Scala and the following example: val values = Set(1, 2, 3) val results = for {v <- values} yield (v * 2) Can anyone explain how Scala knows which type of collection to yield into? I know it is based on values, but…
Zecrates
  • 2,952
  • 6
  • 33
  • 50
16
votes
1 answer

Difference between yield [] & yield all() - ES6/redux-saga

Is there any advantage in using redux-saga's yield all([]) over ES6's built-in yield []? To run multiple operations in parallel, redux-saga suggests: const result = yield all([ call(fetchData), put(FETCH_DATA_STARTED), ]); But the same can be…
Ali Saeed
  • 1,519
  • 1
  • 16
  • 23
16
votes
3 answers

Yield Request call produce weird result in recursive method with scrapy

I'm trying to scrap all departures and arrivals in one day from all airports in all country using Python and Scrapy. The JSON database used by this famous site (flight radar) need to query page by page when departure or arrival is > 100 in one…
reyman64
  • 523
  • 4
  • 34
  • 73
16
votes
3 answers

F# yield! operator - Implementation and possible C# equivalents

I'm currently learning F# and I really love the yield! (yield-bang) operator. Not only for its name but also for what it does of course. The yield! operator basically allows you to yield all elements of a sequence from a sequence expression. This is…
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
16
votes
4 answers

Serialization and the Yield statement

Is it possible to serialize a method containing yield statements (or a class that contains such a method) such that when you rehydrate the class, the internal state of the generated iterator is retained?
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
16
votes
2 answers

Calling coroutines in asyncio.Protocol.data_received

I am having a problem doing asynchronous stuff in the asyncio.Protocol.data_received callback of the new Python asyncio module. Consider the following server: class MathServer(asyncio.Protocol): @asyncio.coroutine def slow_sqrt(self, x): …
oberstet
  • 21,353
  • 10
  • 64
  • 97
15
votes
4 answers

When is the Enumerator::Yielder#yield method useful?

The question "Meaning of the word yield" mentions the Enumerator::Yielder#yield method. I haven't used it before, and wonder under what circumstances it would be useful. Is it mainly useful when you want to create an infinite list of items, such as…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
15
votes
4 answers

Using yield to iterate over a datareader might not close the connection?

Here is a sample code to retrieve data from a database using the yield keyword that I found in a few place while googling around : public IEnumerable ExecuteSelect(string commandText) { using (IDbConnection connection =…
Joel Gauvreau
  • 3,586
  • 4
  • 29
  • 32
15
votes
2 answers

What is the difference between "yield return 0" and "yield return null" in Coroutine?

I'm new and a bit confused about "yield". But finally I understand how it worked using WaitForSeconds but I can't see the difference between of "yield return 0" and "yield return null". are both them waiting for the next frame to execute? sorry for…
yumugee
  • 993
  • 3
  • 9
  • 13
15
votes
4 answers

generator/block to iterator/stream conversion

Basically I want to convert this: def data(block: T => Unit) to a Stream (dataToStream is a hypothetical function that do this conversion): val dataStream: Stream[T] = dataToStream(data) I suppose this problem could be resolved by…
Dawid Grzesiak
  • 402
  • 4
  • 9
15
votes
1 answer

Thread.yield() considered harmful?

While working on my Java application, I've a simple multithreading case (one asynchronous resource loader thread and one main thread waiting for the loader to finish, updating the UI with the progress), which I figured to solve by calling while(…
user719662