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
33
votes
11 answers

C# IEnumerator/yield structure potentially bad?

Background: I've got a bunch of strings that I'm getting from a database, and I want to return them. Traditionally, it would be something like this: public List GetStuff(string connectionString) { List categoryList = new…
Beska
  • 12,445
  • 14
  • 77
  • 112
33
votes
3 answers

Is Yield Return == IEnumerable & IEnumerator?

Is yield return a shortcut for implementing IEnumerable and IEnumerator?
John
  • 5,381
  • 4
  • 30
  • 26
32
votes
4 answers

Python 3.x: Test if generator has elements remaining

When I use a generator in a for loop, it seems to "know", when there are no more elements yielded. Now, I have to use a generator WITHOUT a for loop, and use next() by hand, to get the next element. My problem is, how do I know, if there are no more…
madamada
  • 323
  • 1
  • 3
  • 5
32
votes
4 answers

Does the C# Yield free a lock?

I have the following method: public static IEnumerable> GetRowsIter (this SqlCeResultSet resultSet) { // Make sure we don't multi thread the database. lock (Database) { if (resultSet.HasRows) …
Vaccano
  • 78,325
  • 149
  • 468
  • 850
32
votes
4 answers

How to "yield put" in redux-saga within a callback?

Because "yield"-statement isn't allowed within a callback, how can i use the "put" feature of redux-saga within a callback? I'd like to have the following callback: function onDownloadFileProgress(progress) { yield put({type: ACTIONS.S_PROGRESS,…
delete
  • 18,144
  • 15
  • 48
  • 79
32
votes
7 answers

How can I traverse a file system with a generator?

I'm trying to create a utility class for traversing all the files in a directory, including those within subdirectories and sub-subdirectories. I tried to use a generator because generators are cool; however, I hit a snag. def…
Evan Kroske
  • 4,506
  • 12
  • 40
  • 59
31
votes
5 answers

Return or yield from a function that calls a generator?

I have a generator generator and also a convenience method to it - generate_all. def generator(some_list): for i in some_list: yield do_something(i) def generate_all(): some_list = get_the_list() return generator(some_list) # <-- Is this…
hyankov
  • 4,049
  • 1
  • 29
  • 46
31
votes
3 answers

Python: yield and yield assignment

How does this code, involving assignment and the yield operator, work? The results are rather confounding. def test1(x): for i in x: _ = yield i yield _ def test2(x): for i in x: _ = yield i r1 =…
Charlie Haley
  • 4,152
  • 4
  • 22
  • 36
31
votes
6 answers

Is yield return in C# thread-safe?

I have the following piece of code: private Dictionary items = new Dictionary; public IEnumerable Keys { get { foreach (object key in items.Keys) { yield return key; …
Albic
  • 3,559
  • 4
  • 22
  • 25
30
votes
2 answers

yield from vs yield in for-loop

My understanding of yield from is that it is similar to yielding every item from an iterable. Yet, I observe the different behavior in the following example. I have Class1 class Class1: def __init__(self, gen): self.gen = gen …
erzya
  • 598
  • 4
  • 9
30
votes
8 answers

Why wasn't yield added to C++0x?

EDIT, 11 years after I asked this question: I feel vindicated for asking! C++20 finally did something close enough. The original question follows below. -- I have been using yield in many of my Python programs, and it really clears up the code in…
ttsiodras
  • 10,602
  • 6
  • 55
  • 71
28
votes
3 answers

Is it possible to use 'yield' to generate 'Iterator' instead of a list in Scala?

Is it possible to use yield as an iterator without evaluation of every value? It is a common task when it is easy to implement complex list generation, and then you need to convert it into Iterator, because you don't need some results...
yura
  • 14,489
  • 21
  • 77
  • 126
28
votes
14 answers

Is yield useful outside of LINQ?

When ever I think I can use the yield keyword, I take a step back and look at how it will impact my project. I always end up returning a collection instead of yeilding because I feel the overhead of maintaining the state of the yeilding method…
Bob
  • 97,670
  • 29
  • 122
  • 130
28
votes
7 answers

Javascript check yield support

I read about the yield keyword in JavaScript and I need to use it in my project. I read that this keyword has been implemented starting from a certain version of JS so I think that old browsers don't support it (right?). Is there a way to check if…
mck89
  • 18,918
  • 16
  • 89
  • 106
28
votes
9 answers

Can someone demystify the yield keyword?

I have seen the yield keyword being used quite a lot on Stack Overflow and blogs. I don't use LINQ. Can someone explain the yield keyword? I know that similar questions exist. But none really explain what is its use in plain simple language.
Sandbox
  • 7,910
  • 11
  • 53
  • 67