yield

Does Scala have an equivalent to C# yield?

泄露秘密 提交于 2019-11-27 20:29:51
I'm new to Scala, and from what I understand yield in Scala is not like yield in C#, it is more like select. Does Scala have something similar to C#'s yield? C#'s yield is great because it makes writing iterators very easy. Update: here's a pseudo code example from C# I'd like to be able to implement in Scala: public class Graph<T> { public IEnumerable<T> BreadthFirstIterator() { List<T> currentLevel = new List<T>(); currentLevel.add(_root); while ( currentLevel.count > 0 ) { List<T> nextLevel = new List<T>(); foreach( var node in currentLevel ) { yield return node; nextLevel.addRange( node

How yield catches StopIteration exception?

烂漫一生 提交于 2019-11-27 20:19:36
问题 Why in the example function terminates: def func(iterable): while True: val = next(iterable) yield val but if I take off yield statement function will raise StopIteration exception? EDIT: Sorry for misleading you guys. I know what generators are and how to use them. Of course when I said function terminates I didn't mean eager evaluation of function. I just implied that when I use function to produce generator: gen = func(iterable) in case of func it works and returns the same generator, but

函数进阶-生成器

拥有回忆 提交于 2019-11-27 19:47:15
一丶yield关键字 yield的英文单词意思是生产,在函数中但凡出现yield关键字,再调用函数,就不会继续执行函数体代码,而是会返回一个值。 def func(): print(1) yield print(2) yield g = func() print(g) <generator object func at 0x10ddb6b48> 生成器的本质就是迭代器,同时也并不仅仅是迭代器,不过迭代器之外的用途实在是不多,所以我们可以大声地说:生成器提供了非常方便的自定义迭代器的途径。并且从Python 2.5+开始,[PEP 342:通过增强生成器实现协同程序]的实现为生成器加入了更多的特性,这意味着生成器还可以完成更多的工作。这部分我们会在稍后的部分介绍。 def func(): print('from func 1') yield 'a' print('from func 2') yield 'b' g = func() print(F"g.__iter__ == g: {g.__iter__() == g}") res1 = g.__next__() print(f"res1: {res1}") res2 = next(g) print(f"res2: {res2}") # next(g) # StopIteration g.__iter__ == g: True

What is the result of a yield expression in Python?

Deadly 提交于 2019-11-27 18:27:37
I know that yield turns a function into a generator, but what is the return value of the yield expression itself? For example: def whizbang(): for i in range(10): x = yield i What is the value of variable x as this function executes? I've read the Python documentation: http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt and there seems to be no mention of the value of the yield expression itself. You can also send values to generators. If no value is sent then x is None , otherwise x takes on the sent value. Here is some info: http://docs.python.org/whatsnew/2.5.html

Recursion using yield

老子叫甜甜 提交于 2019-11-27 18:16:55
Is there any way to mix recursion and the yield statement? For instance, a infinite number generator (using recursion) would be something like: def infinity(start): yield start # recursion here ... >>> it = infinity(1) >>> next(it) 1 >>> next(it) 2 I tried: def infinity(start): yield start infinity(start + 1) and def infinity(start): yield start yield infinity(start + 1) But none of them did what I want, the first one stopped after it yielded start and the second one yielded start , then the generator and then stopped. NOTE: Please, I know you can do this using a while-loop: def infinity(start

Continuations and for comprehensions — what's the incompatibility?

孤者浪人 提交于 2019-11-27 18:00:02
问题 I am new to Scala and trying to wrap my head around continuations I'm trying to reproduce the yield return C# statement. Following this post, I have written the following code : package com.company.scalatest import scala.util.continuations._; object GenTest { val gen = new Generator[Int] { def produce = { yieldValue(1) yieldValue(2) yieldValue(3) yieldValue(42) } } // Does not compile :( // val gen2 = new Generator[Int] { // def produce = { // var ints = List(1, 2, 3, 42); // // ints.foreach(

C#: yield return range/collection

旧时模样 提交于 2019-11-27 17:05:28
问题 I use the yield return keyword quite a bit, but I find it lacking when I want to add a range to the IEnumerable . Here's a quick example of what I would like to do: IEnumerable<string> SomeRecursiveMethod() { // some code // ... yield return SomeRecursiveMethod(); } Naturally this results in an error, which can be resolved by doing a simple loop. Is there a better way to do this? A loop feels a bit clunky. 回答1: No, there isn't I'm afraid. F# does support this with yield! , but there's no

生成器进阶

笑着哭i 提交于 2019-11-27 16:47:30
send与next def generator(): print(123) count = yield 1 print('=====',count) print(456) yield 2 g = generator() ret = g.__next__() print('****',ret) ret = g.send('套你大象')#send的效果与next一样 print('***',ret) View Code send在获取下一个值时,会在上一个yield值之后传一个值进来 send获取下一个值的效果和next效果基本一致 只是在获取下个值的时候,就给上一个值的位置传递一个数据。 send的注意事项  1. 第一次使用生成器的时候,必须使用next获取下一个值。  2. 最后一个yield不能接收外部的值 来源: https://www.cnblogs.com/zly9527/p/11370721.html

What are real life applications of yield?

丶灬走出姿态 提交于 2019-11-27 16:07:45
问题 I know what yield does, and I've seen a few examples, but I can't think of real life applications, have you used it to solve some specific problem? (Ideally some problem that cannot be solved some other way) 回答1: I realise this is an old question (pre Jon Skeet?) but I have been considering this question myself just lately. Unfortunately the current answers here (in my opinion) don't mention the most obvious advantage of the yield statement. The biggest benefit of the yield statement is that

python之爬虫 yield关键字

末鹿安然 提交于 2019-11-27 15:55:49
yield —> 生成器 是什么? 生成器是一个不断产生值得函数 包含yield语句得函数是一个生成器 生成器每产生一个值(yield语句),函数就会被冻结,被唤醒后再产生一个值 e.g. def gen ( n ) : for i in range ( n ) : yield i ** 2 输出结果为: 0 1 4 9 16 为啥要用生成器? 生成器相比一次列出所有内容的优势 更节省存储空间 响应更迅速 使用更灵活 永远只需要一个元素的存储空间 来源: https://blog.csdn.net/qq_44790423/article/details/99694801