yield

Why does an IEnumerator have to have at least one yield statement, even if it's unreachable?

可紊 提交于 2019-12-01 21:56:37
Why does this code: public IEnumerator Test() { } Gives you an error: Error CS0161 'Test.GetEnumerator()': not all code paths return a value However this code: public IEnumerator Test() { if(false) yield return 0; } Doesn't? (and works as expected; first MoveNext() returns false) When using IEnumerators as coroutines, sometimes you want to make a coroutine (IEnumerator) that doesn't have an async operations yet (is not yielding anything) but might do that in future. From C# specification: A block that contains one or more yield statements (§8.14) is called an iterator block. Iterator blocks

Rewrite this foreach yield to a linq yield?

孤街醉人 提交于 2019-12-01 18:01:12
Say I have the following code (context narrowed down as to keep the question scope limited) public static IEnumerable<Color> GetThemColors(){ var ids = GetThePrimaryIds(); foreach (int id in ids){ yield return GetColorById(id); } ids = GetTheOtherIds(); foreach (int id in ids){ yield return GetOtherColorsById(id); } } I would like to rewrite them to something like this (which off course doesn't compile public static IEnumerable<Color> GetThemColors(){ GetThePrimaryIds().Select(id=>yield return GetColorById(id)); GetTheOtherIds().Select(id=>yield return GetOtherColorsById(id)); } The key point

Rewrite this foreach yield to a linq yield?

雨燕双飞 提交于 2019-12-01 17:41:12
问题 Say I have the following code (context narrowed down as to keep the question scope limited) public static IEnumerable<Color> GetThemColors(){ var ids = GetThePrimaryIds(); foreach (int id in ids){ yield return GetColorById(id); } ids = GetTheOtherIds(); foreach (int id in ids){ yield return GetOtherColorsById(id); } } I would like to rewrite them to something like this (which off course doesn't compile public static IEnumerable<Color> GetThemColors(){ GetThePrimaryIds().Select(id=>yield

Issue with a python function returning a generator or a normal object

删除回忆录丶 提交于 2019-12-01 17:33:34
I defined the function f as def f(flag): n = 10 if flag: for i in range(n): yield i else: return range(n) But f returns a generator no matter what flag is: >>> f(True) <generator object f at 0x0000000003C5EEA0> >>> f(False) <generator object f at 0x0000000007AC4828> And if I iterate over the returned object: # prints normally for i in f(True): print(i) # doesn't print for i in f(False): print(i) It looks like f(False) returns a generator which has been iterated over. What's the reason? Thank you. A function containing a yield statement always returns a generator object. Only when you iterate

Generator函数

谁说胖子不能爱 提交于 2019-12-01 16:58:29
Generator函数是ES6提供的一种 异步编程 解决方案,语法行为与传统函数完全不同。 一、语法 二、API 三、应用 四、异步应用 一、语法 Generator函数是一个状态机,封装了多个内部状态。执行Generator函数会返回一个遍历器对象,返回的遍历器对象可以依次遍历Generator函数内部的每一个状态。 Generator 函数是一个普通函数,但是有两个特征。一是, function 关键字与函数名之间有一个星号;二是,函数体内部使用 yield 表达式,定义不同的内部状态。 function* helloWorldGenerator() { yield 'hello'; yield 'world'; return 'ending'; } var hw = helloWorldGenerator(); 上面代码定义了一个 Generator 函数 helloWorldGenerator ,它内部有两个 yield 表达式( hello 和 world ),即该函数有三个状态:hello,world 和 return 语句(结束执行)。 调用 Generator 函数后,该函数并不执行,而是返回一个指向内部状态的指针对象。下一步,必须调用遍历器对象的 next 方法,使得指针移向下一个状态。Generator 函数是分段执行的, yield 表达式是暂停执行的标记,而

Ruby equivalent of C#'s 'yield' keyword, or, creating sequences without preallocating memory

瘦欲@ 提交于 2019-12-01 16:30:08
问题 In C#, you could do something like this: public IEnumerable<T> GetItems<T>() { for (int i=0; i<10000000; i++) { yield return i; } } This returns an enumerable sequence of 10 million integers without ever allocating a collection in memory of that length. Is there a way of doing an equivalent thing in Ruby? The specific example I am trying to deal with is the flattening of a rectangular array into a sequence of values to be enumerated. The return value does not have to be an Array or Set , but

What happens when a Python yield statement has no expression?

柔情痞子 提交于 2019-12-01 16:21:07
I'm a C# programmer trying to understand some Python code. The code in question is a generator function, and looks like this: def func(): oldValue = curValue yield curValue = oldValue If I understand this correctly, this will generate a iterable sequence with one member. However, there is no expression after the yield statement. What is such an expression-less statement supposed to yield? Are there any Python idioms that make use of this way of coding? It'll yield None ; just like an empty return expression would: >>> def func(): ... yield ... >>> f = func() >>> next(f) is None True You'd use

python第九期学习笔记(六)(生成器)

こ雲淡風輕ζ 提交于 2019-12-01 16:12:29
yield与return的区别: return一般在函数中只设置一个,他的作用是终止函数,并且给函数的执行者返回值。 yield在生成器函数中可设置多个,他并不会终止函数,next会获取对应yield生成的元素。 def eat(): for i in range(1,10000): yield "包子"+str(i)e=eat()for i in range(2000): print(next(e)) 来源: https://www.cnblogs.com/gaoyuxia/p/11692233.html

What happens when a Python yield statement has no expression?

荒凉一梦 提交于 2019-12-01 15:03:38
问题 I'm a C# programmer trying to understand some Python code. The code in question is a generator function, and looks like this: def func(): oldValue = curValue yield curValue = oldValue If I understand this correctly, this will generate a iterable sequence with one member. However, there is no expression after the yield statement. What is such an expression-less statement supposed to yield? Are there any Python idioms that make use of this way of coding? 回答1: It'll yield None ; just like an

生成器和生成器表达式

孤街浪徒 提交于 2019-12-01 13:46:15
# 生成器本质就是迭代器 # 第一种方法通过函数来获取生成器 def fun1(): print('111') yield '222' print('333') yield '444' g = fun1() # 函数不会执行而是获取生成器 print(g) # fun1是生成器函数,而g就是生成器 <generator object fun1 at 0x000001D1E2F1B3C8> print(g.__next__()) print(g.__next__()) # 当执行nextAPI的时候函数才会执行,yield是返回值 # yield和return都是来返回值区别是什么?yield是分段执行函数而return是直接停止函数 # 函数生成器可以使用send方法传值 def fun2(): a = yield '222' print('a=',a) b = yield '444' print('b=',b) yield '666' g = fun2() print(g.__next__()) print(g.send('首次传值')) print(g.send('二次传值')) # 222 # a= 首次传值 # 444 # b= 二次传值 # 666 # 打印结果,send同样是执行下一段代码,这里next执行的时候,代码到yield '222'停止运行 #