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

Concatenate while yielding

How can I concatenate two items when yielding from a function in python? The base case: import itertools def test(): for number in range(0,10): yield number list(test()) # [0...9] What if I want to yield both the number and its square…
raul ferreira
  • 886
  • 7
  • 21
9
votes
4 answers

Why does yielding to lambda splat array arguments in Ruby?

In Ruby, calling a lambda with the wrong number of arguments results in an ArgumentError: l = lambda { |a, b| p a: a, b: b } l.call(1, 2) # {:a=>1, :b=>2} l.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2) Passing an array…
Stefan
  • 109,145
  • 14
  • 143
  • 218
9
votes
1 answer

PHP yield vs PDO fetch?

Yesterday, I've learned that PHP has a yield() method. I was unure about its usefulness in PHP. A colleague said it could help for SQL statements returning many rows causing potential memory issues. I believe he was refering to fetchAll(). But,…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
9
votes
2 answers

What does the * (star/asterisk) syntax after a yield mean in a recursive generator function?

Let's say I've created an ES6 generator function *createFibonacciIterator(a = 0, b = 1) { yield b; yield *createFib(b, b + a); // <== QUESTION IS ABOUT THIS LINE } Then I use that generator to get the first 20 results let fibber =…
Anthony Astige
  • 1,919
  • 1
  • 13
  • 18
9
votes
3 answers

How yield implements the pattern of lazy loading?

How yield implements the pattern of lazy loading?
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
9
votes
2 answers

Generator recovery using decorator

Let's have a class that has function that fails from time to time but after some actions it just works perfectly. Real life example would be Mysql Query that raises _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away') but after…
Vyktor
  • 20,559
  • 6
  • 64
  • 96
9
votes
2 answers

rspec yield block, but call original

So I have the following: foo.each do |f| f.begin do_stuff do_more_stuff end end And I mock the f object with an and_yield() call. I want to be able to test the begin method by passing it the original block { do_stuff do_more_stuff },…
Nicholas Terry
  • 1,812
  • 24
  • 40
9
votes
2 answers

Match multiple yields in any order

I want to test an iterator using rspec. It seems to me that the only possible yield matcher is yield_successive_args (according to https://www.relishapp.com/rspec/rspec-expectations/v/3-0/docs/built-in-matchers/yield-matchers). The other matchers…
mirelon
  • 4,896
  • 6
  • 40
  • 70
9
votes
1 answer

What happens when a Python yield statement has no expression?

I'm a C# programmer trying to understand some Python code. The code in question is a generator function, and looks like this: def func(): oldValue = curValue yield curValue = oldValue If I understand this correctly, this will generate a…
John Källén
  • 7,551
  • 31
  • 64
9
votes
1 answer

Rewrite simple ruby function to use a block

I do not know the correct terminology. I tried to google it and could not find anything for that reason. I am writing a Ruby library, and I want to rewrite the functions so they work as below as I prefer it for readability (inside a block?) I have a…
ADAM
  • 3,903
  • 4
  • 29
  • 45
9
votes
4 answers

Can a python lambda/fn yield on behalf of an arbitrary caller?

UPDATE: example now lists desired results (boldfaced below) I find myself writing lots of functions that search through some data, where I want to let the caller specify behaviours when matches are found: they might print something out or add it to…
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
9
votes
2 answers

yield - statement or expression?

So, I've been reading this, and found out about sending values to generator. And now I'm kinda confused. Is yield a statement or an expression? It doesn't use parenthesis syntax, like functions, so it looks like statement. But it returns value, so…
Filip Malczak
  • 3,124
  • 24
  • 44
9
votes
3 answers

In there something similar to Java's Thread.yield() in Python? Does that even make sense?

I want to tell my Python threads to yield, and so avoid hogging the CPU unnecessarily. In Java, you could do that using the Thread.yield() function. I don't think there is something similar in Python, so I have been using time.sleep(t) where t =…
Carlos
  • 932
  • 1
  • 7
  • 9
9
votes
4 answers

Scala for-loop. Getting index in consice way

In this code I want to increment index to put it to each yielding result. var index=0 for(str <- splitToStrings(text) ) yield { if (index != 0) index += 1 // but index is equal to `0` all the time new Word(str, UNKNOWN_FORM,…
ses
  • 13,174
  • 31
  • 123
  • 226
9
votes
3 answers

Does thread.yield() lose the lock on object if called inside a synchronized method?

I understand that Thread.currentThread().yield() is a notification to thread scheduler that it may assign cpu cycle to some other thread of same priority if any such is present. My question is: If current thread has got lock on some object and calls…
Amaresh
  • 331
  • 2
  • 5
  • 14