yield

What is the name of the “yield return” concept? IEnumerable, Iterator, Generator?

自作多情 提交于 2019-12-11 06:56:18
问题 What exactly is the official name of the yield return concept? public IEnumerable<T> Bar() { ... yield return foo; ... } I've heard(read) it being referred to as: Iterator IEnumerabe Generator <your choice> Isn't an iterator just some "thing" that iterates over something? A List<T> is an IEnumerable<T> , so doesn't seem reasonable as well. A lot of answers here on SO that talk about yield return use one of these two terms. What about "generators"? Or does that only apply when you're

Python — consuming one generator inside various consumers

流过昼夜 提交于 2019-12-11 03:45:58
问题 I have a generator to be consumed by various consumers. Each of the latter can take different items from the generator, so I can't just use one big for-loop to take care of all the items. What I want is to completely consume the generator. How can it be done? # -*- coding: utf-8 -*- MEALS = ['Oysters', 'Consommé', 'Lamb', 'Rice', 'Sirloin','Banana', 'Pastry'] def server(): for n in MEALS: yield n def client(course, take): meal = [] for _ in range(take): some_meal = next(course) meal.append

Using yield in callback?

半腔热情 提交于 2019-12-11 03:27:03
问题 I have a function y() that is supposed to yield some records. This function however obtains the records within a callback which is passed to another function d() to access the data. d() does not return or yield anything. Is this pattern possible if that other function d() that accepts the callback is considered a black box? What would be an alternative design? function y() { d( function ($records) { // May be called multiple times // How to yield for "y()"? foreach ($records as $record) yield

Why doesn't the Python interpreter implicitly create the generator?

为君一笑 提交于 2019-12-11 03:07:57
问题 #def func(param): # if param < 0: # return "test" # i = 0 # while i < param: # yield i # i += 1 def func(param): if param < 0: return "test" def gen(n): i = 0 while i < param: yield i i += 1 return gen(param) print(func(-1)) print(func(3)) g = func(3) for i in range(0, 3): print(next(g)) Is there a reason that the Python interpreter can not convert the commented code to the actual code implicitly? This seems like this should be allowed, but I am wondering what repercussions there are that

Readable unit testing of a yielded sequence?

半腔热情 提交于 2019-12-11 02:17:14
问题 Let's suppose I have some method that returns a IEnumerable<int> object. This methods make use of yield return keyword to produce a infinite sequence. Example of the Fibonacci algorithm : public static IEnumerable<long> Fibonacci() { long x = 0L; long y = 1L; long z; yield return x; yield return y; while (true) { z = x + y; yield return z; y = x; x = z; } } How can I properly create unit test for such sequence ? By proper I also mean readable. I can write unit tests like this : [TestMethod]

yield return in recursion

故事扮演 提交于 2019-12-11 01:39:09
问题 i am attempting to create an IEnumrable<PropertyInfo> iv'e got a method called Disassemble which iterates recursively throw a given object and all it's child objects of it's properties . please do not concern your self with the Inner wrapper objects of type INameValueWrapper The problem below is when i encounter a property which is a Class i wan't to call Disassemble on it as well and add it to the same iteration of the IEnumrable , the Dissasemble does not occur again when it is called where

Python基础(4)列表及常用操作

断了今生、忘了曾经 提交于 2019-12-10 21:15:08
列表(list) 1.创建列表 (1).直接创建 number = [ 1 , 2 , 3 , 4 , '你好' ] (2).通过list()创建,开发过程中常用来进行其他数据类型向列表数据的强制转换 a = list ( range ( 10 , 20 , 2 ) ) print ( a ) 结果: [ 10 , 12 , 14 , 16 , 18 ] (3).通过表达式创建列表,语法结构[数学表达式 条件1 条件2 条件3…] list1 = [ x ** 2 for x in range ( 1 , 9 ) if x & 1 == 0 ] print ( list1 ) 结果: [ 4 , 16 , 36 , 64 ] (4).生成器 在编程中,我们有时需要生成一个列表,但是该列表可能占据大量的内存,此时列表直接加载到内存中就不再可取了,python提供一个列表生成器,列表生成器是一段生成对应列表的算法。 优点: 节省内存; 提高程序运行效率. 列表生成器每一次迭代,生成器产生的对应的数会被回收。 列表生成器的两种写法: 将列表的 [ ] 改成 () 下面是一个列表: a = [ x for x in range ( 10 ) ] print ( type ( a ) ) 结果: < class 'list' > 此时,将 [ ] 改成 () a = ( x for x in

Ruby on Rails pass reference to yield (template)

不想你离开。 提交于 2019-12-10 19:12:29
问题 To make long story short: each of my tabs has its own form, so I decided to make a single layout and just to have a forms themselves as a variable content for a layout. But I need to have form_for to be in a layout, rather then having it in each of the forms, because I have some other common form elements in the layout. So how can I pass the form builder's reference f to the template ? Layout code: <% content_for(:content) do %> <%= form_for current_form do |f| %> <%= yield %> <%= f.submit

Can params be used to pass variables by ref via a function using yield

回眸只為那壹抹淺笑 提交于 2019-12-10 15:57:08
问题 If I have a method that has a params parameter, can it be passed by reference and updated every time a yield is called. Something like this: public static void GetRowsIter(ref params valuesToUpdate) { foreach(row in rows) { foreach(param in valuesToUpdate { GetValueForParam(param) } yield; } } Is that legal? (I am away from my compiler or I would just try it out.) 回答1: No. params just creates an array that contains the parameters being passed. This array, like all others, is just a collection

Python中yield是什么

谁都会走 提交于 2019-12-10 15:06:36
介绍 我们有时候会发现代码中return的地方,有用yield的,难道他们一样吗?其实,yield与return看起来很像,但实际上完全不同。 使用 def test(): print("****start****") while 1: res = yield 1 print("res:", res) t = test() # 函数中有yield关键字,所以函数并不会真的执行,而是先得到一个生成器t print(next(t)) # next让函数开始执行,打印start,然后把1返回给next(t),并把1打印出来,这时res还没赋值 print("*"*10) # 打印十个* print(next(t)) # 函数从res = yield开始执行,但是1已经被yield出去了,所以res实际上没有值,就是None,然后while重新循环,1被yield出去打印 # ****start**** # 1 # ********** # res:None # 1 你绕出来了吗? 来源: https://www.cnblogs.com/mrdoghead/p/12016486.html