yield-keyword

Can I evaluate a block inside a Proc?

烈酒焚心 提交于 2020-01-01 06:12:49
问题 Can I yield a block inside a Proc? Consider this example: a = Proc.new do yield end a.call do puts "x" end What I'm trying to achieve is to print x , but interpreting this with ruby 2.0 raises LocalJumpError: no block given (yield) . 回答1: No you can't, because the Proc you've created is an independent yield - that is, it's a yield that has no block in its context. Although you can call procs with specified parameters and thereby pass the parameters into the proc, yield doesn't work based on

What does yield mean in PHP?

北城余情 提交于 2019-12-17 04:10:52
问题 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 T_VARIABLE on line x So what is this yield keyword? Is it even valid PHP? And if it is, how do I use it? 回答1: What is yield ? The yield keyword returns data from a generator function: The heart of a generator function is the yield keyword. In its simplest form, a

Generator based Javascript coroutine library supporting Chrome browser

扶醉桌前 提交于 2019-12-13 02:08:00
问题 Javascript generator cannot help too much since it is not a real coroutine. So I hope to have coroutine in browser using some new ecmascript 6 keyword, "yield". i.e., I hope I can yield across multiple frames in the callstack. To my knowledge, I just found a coroutine library based on Javascript 1.7+ on Firefox which can be found at http://www.neilmix.com/2007/02/07/threading-in-javascript-17/. "yield" has been supported in Chrome browser for a long time. So I am wondering there is a

Can generator be used more than once?

纵然是瞬间 提交于 2019-12-12 07:47:40
问题 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 is as expected: 0 1 2 However, if I execute: for x in three_line_gen(): print x for x in three_line_gen(): print x The result is: 0 1 2 0 1 2 Why? I thought any generator can be used only once. 回答1: three_line_gen is not a generator, it's a function. What it returns when

Why is the C# compiler claiming 'use of an unassigned variable' prior to 'yield return' and dynamic?

微笑、不失礼 提交于 2019-12-10 13:56:55
问题 The compiler complains that resultingThing in the code below is being used before being assigned to. private IEnumerable<IThing> FindThings(dynamic spec) { if (spec == null) yield break; IThing resultingThing; if (spec.Something > 0 && dictionary.TryGetValue(spec.Something, out resultingThing)) yield return resultingThing; else // ... } Why does it claim this? I have tried a different version of the method in which there are no yield usages (e.g. just return IEnumerable<IThing> ) but with the

why yield can not work with while loop in scala

血红的双手。 提交于 2019-12-10 09:34:16
问题 In Scala, yield can work with for-loops; for example: val ints: IndexedSeq[Int] = for(i <- 1 to 10) yield i But I found that yield can not work with while-loops, e.g. like: while (resultSet.next()) yield new Row(resultSet) Why is Scala designed like this? I have searched on Google and stackoverflow, but could not find an answer. 回答1: Because a while loop is a java equivalent while loop, and the 'for loop' is translated to function call of: <IndexedSeq>.map (if you use yield) or <IndexedSeq>

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

对着背影说爱祢 提交于 2019-12-09 14:50:25
问题 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 mutable.MutableList[Int] for (n <- 0 until N) { if (n % 2 == 1) { list += n } else { println("skip even number " + n) } } return list } If I omit printing logs, the implementation become very simple: def getOddsWithoutPrint(N: Int) = for (n <- 0 until

When should I use yield and when yield* with Koa.js

半腔热情 提交于 2019-12-07 17:54:01
问题 Browsing through koa samples, docs and middleware, I noticed both forms of yield are being used without any particular difference i noticed. The most extreme case is in koa-mount, where the sample code uses the yield next; form, and the package itself uses yield* several times. Other packages (koa-views for example) also use the yield next form. I understand the difference between the 2 forms as defined by the language, but don't understand how is it that in the context of koa they're used

why yield can not work with while loop in scala

风流意气都作罢 提交于 2019-12-05 15:15:06
In Scala, yield can work with for-loops; for example: val ints: IndexedSeq[Int] = for(i <- 1 to 10) yield i But I found that yield can not work with while-loops, e.g. like: while (resultSet.next()) yield new Row(resultSet) Why is Scala designed like this? I have searched on Google and stackoverflow, but could not find an answer. Because a while loop is a java equivalent while loop, and the 'for loop' is translated to function call of: <IndexedSeq>.map (if you use yield) or <IndexedSeq>.foreach (if you don't care the result). Example Scala Code: class ForVsWhileLoop { val dummy = for(i <- 1 to

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

我与影子孤独终老i 提交于 2019-12-05 00:06:21
问题 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? 回答1: When executing functions that use the yield keyword, they actually return a generator. Generators are a type of iterators. While MATLAB does not provide the syntax for either, you can implement the "iterator interface" yourself. Here is an example similar to xrange function in python: classdef rangeIterator < handle