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
8
votes
5 answers

Performance of nested yield in a tree

I've got a tree-like structure. Each element in this structure should be able to return a Enumerable of all elements it is root to. Let's call this method IEnumerable GetAll(). So if we have A <-- topmost root / \ / \ B C …
mafu
  • 31,798
  • 42
  • 154
  • 247
8
votes
9 answers

Swapping array values with for and yield scala

I am trying to swap every pair of values in my array using for and yield and so far I am very unsuccessful. What I have tried is as follows: val a = Array(1,2,3,4,5) //What I want is Array(2,1,4,3,5) for(i<-0 until (a.length-1,2),r<- Array(i+1,i))…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
7
votes
1 answer

scala - yield syntax

I'm reading a book on scala programming (the Programming in Scala), and I've got a question about the yield syntax. According to the book, the syntax for yield can be expressed like: for clauses yield body but when I try to run the script below,…
Void Main
  • 2,241
  • 3
  • 27
  • 36
7
votes
3 answers

How to have django give a HTTP response before continuing on to complete a task associated to the request?

In my django piston API, I want to yield/return a http response to the the client before calling another function that will take quite some time. How do I make the yield give a HTTP response containing the desired JSON and not a string relating to…
Dangermouse
  • 83
  • 1
  • 5
7
votes
3 answers

Why is yield statement of a javascript generator function returning parameters of .next()?

I stumbled on generator functions on MDN and what puzzles me is the following example: function* logGenerator() { console.log(yield); console.log(yield); console.log(yield); } var gen = logGenerator(); // the first call of next executes from…
jakubiszon
  • 3,229
  • 1
  • 27
  • 41
7
votes
4 answers

Python -- Iterate an iterator twice

Edit: There is a similar question here that deals with iterator resetting. The accepted answer below however addresses the actual issue of nested iterators, and handles an easy to miss issue, whereby the nested iterator doesn't reset. Is there any…
c z
  • 7,726
  • 3
  • 46
  • 59
7
votes
10 answers

Yield keyword value added?

still trying to find where i would use the "yield" keyword in a real situation. I see this thread on the subject What is the yield keyword used for in C#? but in the accepted answer, they have this as an example where someone is iterating around…
leora
  • 188,729
  • 360
  • 878
  • 1,366
7
votes
2 answers

What happens when promise is yielded in javascript?

Didn't find full answer .. What happens when promise is yielded? Is such construction var p = new Promise() p.resolve(value) function * (){ yield p } equivalent to function * (){ yield value } ? UPDATE How to mix different styles of async…
akaRem
  • 7,326
  • 4
  • 29
  • 43
7
votes
1 answer

Use Nightmare.js without ES6 syntax and yield

I built a simple node script using nightmare.js to scrape websites var Nightmare = require('nightmare'); var vo = require('vo'); vo(run)(function(err, result) { if (err) throw err; }); function *run() { var x = Date.now(); var…
Rayjax
  • 7,494
  • 11
  • 56
  • 82
7
votes
4 answers

Rails 3 - yield return or callback won't call in view <%= yield(:sidebar) || render('shared/sidebar') %>

I'm migrating a Website from Rails 2 (latest) to Rails 3 (beta2). Testing with Ruby 1.9.1p378 and Ruby 1.9.2dev (2010-04-05 trunk 27225) Stuck in a situation, i don't know which part will work well. Suspect yield is the problem, but don't know…
rzar
  • 1,236
  • 1
  • 13
  • 9
7
votes
2 answers

How does 'yield' work in tornado when making an asynchronous call?

Recently, I was learning Introduction to Tornado, and I came across the following code: class IndexHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.engine def get(self): query = self.get_argument('q') …
streethacker
  • 308
  • 5
  • 13
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
7
votes
1 answer

Laravel 4 syntax error out-of-the-box

I just installed Laravel 4 (Illuminate) and as I opened the index.php file in a browser, I was met with this error: Parse error: syntax error, unexpected 'yield' (T_YIELD), expecting identifier (T_STRING) in…
Erik Djupvik
  • 1,187
  • 1
  • 10
  • 13
7
votes
3 answers

Passing Parameters to yield in Rails 3 (or is it possible?)

I'm trying to create a dynamic content with yield and content_for. Basically i have bunch of layouts. And i dont want to create bunch of views for each layout. I want to render view parts when they are needed. For different parts of code it is ok.…
Çağdaş
  • 993
  • 1
  • 12
  • 33
7
votes
3 answers

Is 'yield' in Scala equivalent to map function?

I'm starting to study the Scala programming language. I've some grasp of FP languages like Erlang and Haskell and I have a doubt about the meaning of the for/yield expression, like: for (arg <- args) yield arg.length This would collect an array…
Vincenzo Maggio
  • 3,787
  • 26
  • 42