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

How to prevent problems with `return` from block when using Ruby `yield`

As every Ruby programmer eventually discovers, calling blocks or procs that contain return statements can be dangerous as this might exit your current context: def some_method(&_block) puts 1 yield # The following line will never be…
sudoremo
  • 2,274
  • 2
  • 22
  • 39
10
votes
2 answers

Yield continue?

OP edit: This was a bad question predicated upon a misunderstanding of how yield return works. The short answer: If you don't want to yield return something, don't yield return anything. There's yield return to return the next element in the…
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
10
votes
2 answers

Why is a generator produced by yield faster than a generator produced by xrange?

I was investigating Python generators and decided to run a little experiment. TOTAL = 100000000 def my_sequence(): i = 0 while i < TOTAL: yield i i += 1 def my_list(): return range(TOTAL) def my_xrange(): return…
Yan Yi
  • 763
  • 1
  • 10
  • 29
10
votes
5 answers

Events vs. Yield

I have a multithreaded application that spawns threads for several hardware instruments. Each thread is basically an infinite loop (for the lifetime of the application) that polls the hardware for new data, and activates an event (which passes the…
drharris
  • 11,194
  • 5
  • 43
  • 56
10
votes
2 answers

What does a plain yield keyword do in Python?

According to the Python documentation the yield keyword can take an "expression_list", but it is optional: yield_expression ::= "yield" [expression_list] I can find no examples of such usage, either in the Python docs, in any of the answers to…
jalanb
  • 1,097
  • 2
  • 11
  • 37
10
votes
4 answers

How would I yield an immutable.Map in Scala?

I have tried this but it does not work: val map:Map[String,String] = for { tuple2 <- someList } yield tuple2._1 -> tuple2._2 How else would I convert a List of Tuple2s into a Map?
Jasper
  • 1,971
  • 19
  • 34
10
votes
1 answer

PHP Yield, strange behaviour

me and a colleague found a very strange behaviour using the new keyword "yield" in PHP version: 5.5.11 and we want to know if the following is normal: Given is the following code: function yieldTest() { echo 'wtf1'; die('wtf2'); foreach…
Steini
  • 2,753
  • 15
  • 24
10
votes
2 answers

Python generator send: don't yield a new value after a send

This is kind of a weird question so I'll explain: I have a generator like this that is acting as a generator frontend to an IRC server: def irc_iter(): # not the real code, simplified msgs = get_msgs() for msg in msgs: if…
vgel
  • 3,225
  • 1
  • 21
  • 35
10
votes
4 answers

Wrapping fs.readFile in a generator/yield

I'm trying to get my head around generators and yield in JavaScript and Node.js, but having an issue. Ideally, what I'd want to do is wrap fs.readFile with generators/yield, so that I can use it synchronously without blocking anything. I've come up…
balupton
  • 47,113
  • 32
  • 131
  • 182
9
votes
1 answer

Strange behavior with NUnit, ExpectedException & yield return

I have a strange behavior in a tests where I want to test that an exception is thrown when null is passed in as a parameter. When I run the test I get from NUnit: System.ArgumentNullException was expected -- Exception doesn't have a stack…
mr carl
  • 571
  • 2
  • 6
  • 20
9
votes
3 answers

`yield` inside a recursive procedure

Let's say I have a Python list representing ranges for some variables: conditions = [['i', (1, 5)], ['j', (1, 2)]] This represents that variable i ranges from 1 to 5, and inside that loop variable j ranges from 1 to 2. I want a dictionary for each…
Noel Arteche
  • 201
  • 1
  • 3
9
votes
5 answers

Multiple content_for on the same page

I have large block of HTML in my application that I would like to move into a shared template and then use content_for with yields to insert the necessary content. However, if I use it more than once in the same layout file the content_for just…
Jason Yost
  • 4,807
  • 7
  • 42
  • 65
9
votes
6 answers

for..of and the iterator state

Consider this python code it = iter([1, 2, 3, 4, 5]) for x in it: print x if x == 3: break print '---' for x in it: print x it prints 1 2 3 --- 4 5, because the iterator it remembers its state across the loops. When I do…
georg
  • 211,518
  • 52
  • 313
  • 390
9
votes
2 answers

What's the difference between Thread.yield() and Thread.sleep(0) in Java?

Possible Duplicate: Are Thread.sleep(0) and Thread.yield() statements equivalent? In my understanding, both Thread.yield() and Thread.sleep(0) should make the CPU rejudge which thread to run by some scheduling algorithm. The difference…
Hesey
  • 4,957
  • 6
  • 31
  • 31
9
votes
5 answers

Python yield (migrating from Ruby): How can I write a function without arguments and only with yield to do prints?

I've been converting Ruby code to Python code and now I'm stuck with this function that contains yield: def three_print(): yield yield yield I would like to call the function and tell it to print "Hello" three times because of the three…