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

Write one element by row in a csv file with scrapy

I'm scraping this page: http://www.mymcpl.org/cfapps/botb/movie.cfm Extracting four items: book, author, movie,movie_year I want to save this in a CSV file where each row contain records of one movie. This is the spider I wrote: class…
Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181
0
votes
1 answer

Why does the second yield return first in this generator

In the code below, why does the first call to next() return the value that comes after the second yield keyword? function* generatorFunction() { yield (yield 'the second yield')(); } function func(x) { return 'func passed to the second…
David Gilbertson
  • 4,219
  • 1
  • 26
  • 32
0
votes
1 answer

ruby on rails yield to new ruby layout

In a page I'm working on , i have overridden the application.html.erb because the layout is complex for pages like signup / login and some others. So i created a new layout and I'm rendering it through the controller's action. def new render…
Ml Ck
  • 37
  • 7
0
votes
1 answer

Variable assignment takes place in a block in a block

I first had this code, but it wasn't working: VIM = Vimrunner::RSpec.configure do |config| config.reuse_server = true config.start_vim do vim = Vimrunner.start vim end end The configure is just a method that does the settings for a…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
0
votes
2 answers

sinatra: yield the same form multiple times in one layout, iterating names

I'm writing a simple single page app. Right now the form.erb has 16 statically written forms that are all the same except their names and ids. I'd like to make it where form.erb only has one copy of the form but is yielded 16 times iterating the…
iohzrd
  • 113
  • 2
  • 3
0
votes
1 answer

Create a nested objects from for-comprehension in Scala

I have a case class being instantiated based on some DB operations: case class FullFight(fight: FightsRow, firstBoxer: BoxersRow, secondBoxer: BoxersRow, bookiesOdds: Seq[BookiesOddsRow]) val tupledJoin = for { f <- Fights b1 <- Boxers…
Gandalf
  • 155
  • 1
  • 12
0
votes
0 answers

Stream with yielding function is being kept alive after removing the object from an array

I'm using the streaming features of the Twitter library. When a user registers, it begins streaming their tweets: class UserStream def begin_stream() Thread.new do # Setup the client @client = Twitter::Streaming::Client.new do…
Andrew
  • 7,693
  • 11
  • 43
  • 81
0
votes
1 answer

Why does yielding an item in a function give different output than printing the item?

I wrote this function to read a list of words representing a number and pair 1-9 digits with multipliers. def listpairs(element): pair = [] for word in enumerate(element): pair.append(word[1]) if word[1] in denoms: …
cmo
  • 3
  • 2
0
votes
1 answer

Yield a form on a different page using laravel

I'm making a twitter look-a-like app. I have a form to post a message on 1 blade (createMessage.blade.php) which is as followed: @section('message') {!! Form::open(['url' => 'message/postmessage']) !!}
Benny Niemeijer
  • 349
  • 1
  • 3
  • 15
0
votes
1 answer

Python Yield prevents output/execution in Scrapy Web Spider Crawler

I have an issue with my scrapy spider. Ipass a response object from one parse function to another. The code will only execute for functions that that do not have a yield request (see difference between the two snippets) Why is this? # Yield in…
josh
  • 36
  • 2
0
votes
0 answers

How to call generator "companion function"

I was having a hard time understanding ES2015 yield, specifically the fact that you can call an async function with sync style, and most of my question where solved by this answer https://stackoverflow.com/a/18904835/1540114 You can write async…
Gatoyu
  • 642
  • 1
  • 6
  • 22
0
votes
1 answer

Yield not returning value

I'm trying to adapt this code: function pc_permute($items, $perms = array( )) { if (empty($items)) { echo implode($perms); // yield (implode($perms)); } else { for ($i = count($items) - 1; $i >= 0; --$i) { …
Cristo
  • 700
  • 1
  • 8
  • 20
0
votes
1 answer

Python BadYieldError when yielding Future

I'm fairly new to programming using coroutines and I'm trying to build a database interface for a custom database for a Tornado web server using Python 2.7.10. However, I keep getting a BadYieldError. I feel that this is probably me not…
Kemal Ahmed
  • 638
  • 6
  • 15
0
votes
0 answers

How does "line = (yield)" work?

I was reading a Python textbook when I saw this piece of code: def print_matches(matchtext): print "Searching for", matchtext while True: line = (yield) if matchtext in line: print line matcher =…
maryAndreeva
  • 61
  • 1
  • 6
0
votes
1 answer

Usefulness of yield

I was wondering when yield would be useful. It seems to me that I can use Linq everytime I can use yield. Let's say I have this Test class public class Test { public object Test1; public object Test2; public Test(object test1, object…
Arthur Rey
  • 2,990
  • 3
  • 19
  • 42