Questions tagged [yield-keyword]

The yield keyword is used to pause and resume a generator.

From MDN:

The function containing the yield keyword is a generator. When you call it, its formal parameters are bound to actual arguments, but its body isn't actually evaluated. Instead, a generator-iterator is returned. Each call to the generator-iterator's next() method performs another pass through the iterative algorithm. Each step's value is the value specified by the yield keyword. Think of yield as the generator-iterator version of return, indicating the boundary between each iteration of the algorithm. Each time you call next(), the generator code resumes from the statement following the yield.

Yield keyword is used in C#, Phython, JavaScript.

75 questions
261
votes
8 answers

What does yield mean in PHP?

I've recently stumbled over this code: function xrange($min, $max) { for ($i = $min; $i <= $max; $i++) { yield $i; } } I've never seen this yield keyword before. Trying to run the code I get Parse error: syntax error, unexpected…
Gordon
  • 296,205
  • 68
  • 508
  • 534
35
votes
8 answers

What does the "yield" keyword do in Ruby?

I encountered the following Ruby code: class MyClass attr_accessor :items ... def each @items.each{|item| yield item} end ... end What does the each method do? In particular, I don't understand what yield does.
Misha Moroshko
  • 148,413
  • 200
  • 467
  • 700
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,149
  • 19
  • 71
  • 123
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
  • 17,869
  • 15
  • 83
  • 101
20
votes
4 answers

"yield" keyword for C++, How to Return an Iterator from my Function?

Consider the following code. std::vector do_processing() { pqxx::result input_data = get_data_from_database(); return process_data(input_data); } std::vector process_data(pqxx::result const & input_data) { …
moooeeeep
  • 28,315
  • 14
  • 88
  • 166
15
votes
1 answer

yielding from an iterator callback used inside a generator

Has anyone tried to get Underscore JS or lodash (or any ES5 standard functions for that matter) working with generators? If we have an array var myArray = [1,2,3,4,6]; We want to forEach over it. In a non generator case you would…
Sean Clark
  • 1,296
  • 15
  • 30
14
votes
2 answers

What's the advantage of using yield in __iter__()?

What is the advantage of using an generator(yield) inside an __iter__() function? After reading through Python Cookbook I understand "If you want a generator to expose extra state to the user, don’t forget that you can easily implement it as a…
Joe_12345
  • 381
  • 5
  • 15
14
votes
5 answers

Can generator be used more than once?

This is my piece of code with two generators defined: one_line_gen = (x for x in range(3)) def three_line_gen(): yield 0 yield 1 yield 2 When I execute: for x in one_line_gen: print x for x in one_line_gen: print x The result…
Hash Doe
  • 183
  • 1
  • 7
12
votes
4 answers

Scala - can 'for-yield' clause yields nothing for some condition?

In Scala language, I want to write a function that yields odd numbers within a given range. The function prints some log when iterating even numbers. The first version of the function is: def getOdds(N: Int): Traversable[Int] = { val list = new…
pocorall
  • 1,007
  • 1
  • 8
  • 20
10
votes
1 answer

Python - Understanding the send function of a generator

I'm studying in Python yield and find that yield is not only the way in which generators output a return value but also a way to put values into a generator. For example the following code def f(): print (yield), print 0, print (yield), …
neuront
  • 8,164
  • 4
  • 37
  • 65
9
votes
3 answers

Is "yield keyword" useful outside of an iterator block?

The yield keyword documentation says: The yield keyword signals to the compiler that the method in which it appears is an iterator block. I have encountered code using the yield keyword outside any iterator block. Should this be considered as a…
GETah
  • 19,549
  • 6
  • 51
  • 95
9
votes
1 answer

how generators work in python

I am novice in Python and programming. Generators are a bit too complicated to understand for new programmers. Here's my theory on generator functions in Python: Any function contains a yield statement will return a generator object A generator…
user3786562
  • 101
  • 6
8
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
  • 2,874
  • 1
  • 18
  • 32
7
votes
4 answers

Why does the yield function not require parentheses in Python?

In Python, I have many times seen the yield function used to create a generator. Both this and the print function technically both perform the action of methods because they return a value. However, during the change from Python 2 to Python 3, the…
ytpillai
  • 3,194
  • 25
  • 43
7
votes
2 answers

What is the Matlab equivalent of the yield keyword in Python?

I need to generate multiple results but one at a time, as opposed to everything at once in an array. How do I do that in Matlab with a generator like syntax as in Python?
user3191455
  • 111
  • 1
  • 2
1
2 3 4 5