yield

what does yield as assignment do? myVar = (yield)

梦想的初衷 提交于 2019-11-29 10:44:09
问题 I'm familiar with yield to return a value thanks mostly to this question but what does yield do when it is on the right side of an assignment? @coroutine def protocol(target=None): while True: c = (yield) def coroutine(func): def start(*args,**kwargs): cr = func(*args,**kwargs) cr.next() return cr return start I came across this, on the code samples of this blog, while researching state machines and coroutines. 回答1: The yield statement used in a function turns that function into a "generator"

How yield implements the pattern of lazy loading?

*爱你&永不变心* 提交于 2019-11-29 04:07:52
How yield implements the pattern of lazy loading ? yield implementation doesn't reach the code until it's needed. For example, this code: public IEnumerable<int> GetInts() { yield return 1; yield return 2; yield return 3; } Will actually compile into a nested class which implements IEnumerable<int> and the body of GetInts() will return an instance of that class. Using reflector you can see: public IEnumerable<int> GetInts() { <GetInts>d__6d d__d = new <GetInts>d__6d(-2); d__d.<>4__this = this; return d__d; } Edit - adding more info about GetInts implementation: The way this implementation

Continuations and for comprehensions — what's the incompatibility?

我是研究僧i 提交于 2019-11-29 04:01:32
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((theInt) => yieldValue(theInt)); // } // } // But this works? val gen3 = new Generator[Int] { def

试试 IEnumerable 的 10 个小例子

╄→гoц情女王★ 提交于 2019-11-29 03:20:14
IEnumerable 接口是 C# 开发过程中非常重要的接口,对于其特性和用法的了解是十分必要的。本文将通过10个小例子,来熟悉一下其简单的用法。 全是源码 以下便是这10个小例子,响应的说明均标记在注释中。 每个以 TXX 开头命名的均是一个示例。建议从上往下阅读。 using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using FluentAssertions; using Xunit; using Xunit.Abstractions; namespace Try_More_On_IEnumerable { public class EnumerableTests { private readonly ITestOutputHelper _testOutputHelper; public EnumerableTests( ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; } [Fact] public

试试 IEnumerable 的 10 个小例子

非 Y 不嫁゛ 提交于 2019-11-29 03:19:10
IEnumerable 接口是 C# 开发过程中非常重要的接口,对于其特性和用法的了解是十分必要的。本文将通过10个小例子,来熟悉一下其简单的用法。 全是源码 以下便是这10个小例子,响应的说明均标记在注释中。 每个以 TXX 开头命名的均是一个示例。建议从上往下阅读。 using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using FluentAssertions; using Xunit; using Xunit.Abstractions; namespace Try_More_On_IEnumerable { public class EnumerableTests { private readonly ITestOutputHelper _testOutputHelper; public EnumerableTests( ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; } [Fact] public

Loop over two generator together

拈花ヽ惹草 提交于 2019-11-29 03:06:05
I have two generators say A() and B() . I want to iterate over both the generators together. Something like: for a,b in A(),B(): # I know this is wrong #do processing on a and b One way is to store the results of both the functions in lists and then loop over the merged list. Something like this: resA = [a for a in A()] resB = [b for b in B()] for a,b in zip(resA, resB): #do stuff If you are wondering, then yes both the functions yield equal number of value. But I can't use this approach because A()/B() returns so many values. Storing them in a list would exhaust the memory, that's why I am

C#: yield return range/collection

▼魔方 西西 提交于 2019-11-29 02:48:30
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. No, there isn't I'm afraid. F# does support this with yield! , but there's no equivalent in C# - you have to use the loop, basically. Sorry... I feel your pain. I mentioned it in one of my

Are there better ways to prevent 'yield' when no block is passed in?

纵然是瞬间 提交于 2019-11-29 02:09:00
问题 I have a method that yields, which looks like: def a_method(*params) # do something yield # do something else end I want this method to yield the block if a block is passed in; and if no block is passed in, the method should sliently skip the yield sentense without crashing with something like: no block given (yield) (LocalJumpError) Of course, the most straightforward way is changing the method to: def a_method(*params, &block) # do something yield if block # do something else end But aren't

PHP学习之迭代生成器

血红的双手。 提交于 2019-11-29 01:45:24
生成器的核心是一个yield关键字,一个生成器函数看起来像一个普通的函数,不同的是。普通函数返回一个值,而一个生成器可以yield生成许多它所需要的值。生成器函数被调用时,返回的是一个可以被遍历的对象。 yield和return有点类似,不过不同的是,return会返回值并且终止代码的执行,而yield会返回一个值给循环调用此生成器的代码并且只是暂停执行生成器函数。 <?php function get_one_to_three(){ for($i=1;$i<=3;$i++){ yield $i; } } $generator=get_one_to_three(); var_dump($generator); echo '<br/>'; var_dump($generator instanceof Iterator); echo '<br/>'; foreach ($generator as $value){   echo $value,'<br/>'; } 输出结果: 调用get_one_to_three()的时候,里面的代码并没有真正的执行,而是返回了一个生产期对象$generator=Generator Object(),$genetator instanceof Iterator说明Generator实现了Iterator接口,可以用foreach进行遍历

深入理解python的yield和generator

断了今生、忘了曾经 提交于 2019-11-29 01:44:54
原文发表在 我的博客主页 ,转载请注明出处 前言 没有用过的东西,没有深刻理解的东西很难说自己会,而且被别人一问必然破绽百出。虽然之前有接触过python协程的概念,但是只是走马观花,这两天的一次交谈中,别人问到了协程,顿时语塞,死活想不起来曾经看过的东西,之后突然想到了yield,但为时已晚,只能说概念不清,所以本篇先缕缕python的生成器和yield关键字。 什么是生成器 生成器是一个特殊的程序,可以被用作控制循环的迭代行为 生成器类似于返回值为数组的一个函数,这个函数可以接收参数,可以被调用,但是,不同于一般的函数会一次性返回包含了所有数值的数组,生成器一次只产生一个值,这样消耗的内粗数量大大减少,而且允许调用函数可以很快的开始处理前几个返回值。因此,生成器看起来像一个函数但是表现的却像一个迭代器。 python中的生成器 python提供了两种基本的方式。 生成器函数:也是用def来定义,利用关键字 yield 一次返回一个结果,阻塞,重新开始 生成器表达式:返回一个对象,这个对象只有在需要的时候才产生结果 下面详细讲解。 生成器函数 为什么叫生成器函数?因为他随着时间的推移生成了一个数值队列。一般的函数在执行完毕之后会返回一个值然后退出,但是生成器函数会自动挂起,然后重新拾起继续执行,他会利用 yield 关键字关起函数,给调用者返回一个值,同时保留了当前的足够多的状态