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

Why is using a sequence so much slower than using a list in this example

Background: I have a sequence of contiguous, time-stamped data. The data-sequence has holes in it, some large, others just a single missing value. Whenever the hole is just a single missing value, I want to patch the holes using a dummy-value…
Treefrog
  • 403
  • 4
  • 9
19
votes
6 answers

Does python yield imply continue?

I have a for loop that checks a series of conditions. On each iteration, it should yield output for only one of the conditions. The final yield is a default, in case none of the conditions are true. Do I have to put a continue after each block of…
345871345
  • 251
  • 1
  • 2
  • 4
19
votes
2 answers

TypeError: 'generator' object has no attribute '__getitem__'

I have written a generating function that should return a dictionary. however when I try to print a field I get the following error print row2['SearchDate'] TypeError: 'generator' object has no attribute '__getitem__' This is my code from csv…
MAS
  • 4,503
  • 7
  • 32
  • 55
19
votes
2 answers

ES6 Generators: poor stack trace from iterator.throw(err)

The ES6 method: iterator.throw(err) is often described as injecting an exception as though it occurred at the yield statement in the generator. The problem is that the stack trace for this exception does not contain any reference to the file/line…
Avalanche
  • 211
  • 1
  • 6
18
votes
2 answers

Why is the compiler-generated enumerator for "yield" not a struct?

The compiler-generated implementation of IEnumerator / IEnumerable for yield methods and getters seems to be a class, and is therefore allocated on the heap. However, other .NET types such as List specifically return struct enumerators to avoid…
Lazlo
  • 8,518
  • 14
  • 77
  • 116
18
votes
3 answers

PHP generator yield the first value, then iterate over the rest

I have this code: current(); echo $first . '
'; //$gen->next(); foreach ($gen as $value)…
Lay András
  • 795
  • 2
  • 9
  • 14
18
votes
4 answers

Multithreading, when to yield versus sleep

To clarify terminology, yield is when thread gives up its time slice. My platform of interest is POSIX threads, but I think the question is general. Suppose I have consumer/producer pattern. If I want to throttle either consumer or producer, which…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
18
votes
2 answers

Is there a way to test if a closure is also a generator?

I am working with a PHP class which needs to accept multiple types of iterators and encompass them inside a unified wrapper. One of the types of iterators I need to support (and can!) is an anonymous function containing the yield keyword -- an…
dvlsg
  • 5,378
  • 2
  • 29
  • 34
18
votes
2 answers

Python - is there any way to organize a group of yields in sub function to yield outside the main function?

I have a newbie question for python gurus. I have function A that hold a lot of repeated yield-actions like so: yield a yield b yield c so it looks like: def funA(): … yield a yield b yield c … yield a yield b yield c …
Eugene Krevenets
  • 1,791
  • 2
  • 20
  • 35
18
votes
1 answer

TypeError: 'generator' object is not callable

I have a generator defined like this: def lengths(x): for k, v in x.items(): yield v['time_length'] And it works, calling it with for i in lengths(x): print i produces: 3600 1200 3600 300 which are the correct numbers. However,…
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
18
votes
6 answers

Scala - can yield be used multiple times with a for loop?

An example: val l = List(1,2,3) val t = List(-1,-2,-3) Can I do something like this? for (i <- 0 to 10) yield (l(i)) yield (t(i)) Basically I want to yield multiple results for every iteration.
Monis Iqbal
  • 1,987
  • 7
  • 26
  • 42
17
votes
3 answers

Pthread - What is the difference between time.h::sleep() and pthread.h::pthread_yield()?

I spent a good long while looking for info on the differences between time.h::sleep() and pthread.h::pthread_yield() but was unable to find any solid reference material and so I am posting this question. What is the difference between…
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
17
votes
2 answers

TypeScript: Use types on call() from redux-saga

How can I set the types of a function using call()? I have this function: export function apiFetch(url: string): Promise { return fetch(url).then(response => { if (!response.ok) throw new Error(response.statusText) …
Rumpelstinsk
  • 3,107
  • 3
  • 30
  • 57
17
votes
1 answer

F# yield! (yieldbang) operator

I am learning F# at the moment but I'm having a hard time understanding this: let allPrimes = let rec allPrimes' n = seq { if isPrime n then yield n yield! allPrimes' (n + 1) } allPrimes' 2 I am not able to figure…
schgab
  • 521
  • 5
  • 19
17
votes
3 answers

Trouble understanding yield in C#

I'm hoping to get some clarification on a snippet that I've recently stepped through in the debugger, but simply cannot really understand. I'm taking a C# course on PluralSight and the current topic is on yield and returning a IEnumerable with…
geostocker
  • 1,190
  • 2
  • 17
  • 29