yield

How yield implements the pattern of lazy loading?

自古美人都是妖i 提交于 2019-11-30 07:27:35
问题 How yield implements the pattern of lazy loading ? 回答1: 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 =

Python: generator expression vs. yield

谁都会走 提交于 2019-11-30 06:11:55
问题 In Python, is there any difference between creating a generator object through a generator expression versus using the yield statement? Using yield : def Generator(x, y): for i in xrange(x): for j in xrange(y): yield(i, j) Using generator expression : def Generator(x, y): return ((i, j) for i in xrange(x) for j in xrange(y)) Both functions return generator objects, which produce tuples, e.g. (0,0), (0,1) etc. Any advantages of one or the other? Thoughts? Thanks everybody! There is a lot of

迭代器和生成器的用法

 ̄綄美尐妖づ 提交于 2019-11-30 05:41:12
首先在了解解析式之前,我们先来看一个列子:一个列表,元素是0-9,列表中的每个值自增1,该如何实现: 方法一:遍历列表,对其元素进行加1操作后放到一个新的列表中 1 lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2 3 for index, i in enumerate(lst): 4 lst[index] += 1 5 print(lst) 方法二:通过map函数来实现 a = map(lambda x:x+1, lst) print(a) for x in a: print(x) 方法三:通过列表解析式,一行搞定 1 lst2 = [x+1 for x in lst] 2 print(lst2)   方法三就是列表解析式的写法,返回一个新的列表。   那么什么是生成器呢?通过列表解析式我们可以发现,它会直接创建一个新的列表,这样不好的地方就是占用内存,我们知道内存是有限的,如果列表中的元素有几百万,而有时候就仅仅需要个别的数据,那么就会大大浪费内存的空间。   所以,我们是否可以想一种办法来解决这个问题呢?在Python中,生成器就很好的解决了这个问题。我们可以假设列表中的元素能否通过某种算法来推算出来呢?在需要某个数据的时候通过计算来得到这个数据,这样就不会直接生成一个列表来存储许多无用的数据了。在python中,这种一边循环一边计算的机制

Ruby on rails: Yielding specific views in a specific places in the layout

余生颓废 提交于 2019-11-30 03:54:38
If I have one <%= yield %> tag then all my views render in the same place in the layout. Can I have different <%= yield %> tags for different views? Is so how do I do this? Thanks Look into ActionView::Helpers::CaptureHelper . You can do something like this in your views: <% content_for :sidebar do %> <!-- sidebar content specific to this page --> <% end %> This will run the template inside the content_for block, but will not output as part of the regular template yield buffer, it will be stored in a separate buffer for later. Then later on, including in the layout, you can use yield :content

Pthread - What is the difference between time.h::sleep() and pthread.h::pthread_yield()?

柔情痞子 提交于 2019-11-30 03:39:46
I spent a good long while looking for info on the differences between time.h::sleep() and pthread.h::pthread_yield() but was unable to find any solid reference material and so I am posting this question. What is the difference between time.h::sleep() and pthread.h::pthread_yield()? Update: The reason I ask is because I was using sleep() to sleep() each individual thread... and my application started having issues when there was 8 threads vs 4 threads. When I went online to see if sleep() only affects each thread, I couldn't find any good reference stating whether Sleep() affects the entire

Which is generally faster, a yield or an append?

瘦欲@ 提交于 2019-11-30 03:25:13
I am currently in a personal learning project where I read in an XML database. I find myself writing functions that gather data and I'm not sure what would be a fast way to return them. Which is generally faster: yield s, or several append() s within the function then return the ensuing list ? I would be happy to know in what situations where yield s would be faster than append() s or vice-versa. yield has the huge advantage of being lazy and speed is usually not the best reason to use it. But if it works in your context, then there is no reason not to use it: # yield_vs_append.py data = range

How can I tell whether a generator was just-started?

独自空忆成欢 提交于 2019-11-30 02:39:31
I'd like a function, is_just_started , which behaves like the following: >>> def gen(): yield 0; yield 1 >>> a = gen() >>> is_just_started(a) True >>> next(a) 0 >>> is_just_started(a) False >>> next(a) 1 >>> is_just_started(a) False >>> next(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> is_just_started(a) False How can I implement this function? I looked at the .gi_running attribute but it appears to be used for something else. If I know the first value that needs to be sent into the generator, I can do something like this: def safe_send(gen, a):

Fibers in C#: are they faster than iterators, and have people used them?

允我心安 提交于 2019-11-30 02:27:39
So I was chatting with a colleague about fibers and turned up this paper from 2003 that describes a implementation of coroutines in C# using the Fiber API. The implementation of Yield in this paper was for .NET 1.1, so it predates the yield return syntax that appeared in .NET 2.0. It definitely looks, at first glance, that the implementation here is potentially faster and could scale across multiple CPUs rather well. Has anyone used it? I haven't used it, but I have an interest in the subject. Here's one nice implementation of coroutines in C# with a round-robin scheduler: http://www

generator/block to iterator/stream conversion

匆匆过客 提交于 2019-11-30 01:16:40
问题 Basically I want to convert this: def data(block: T => Unit) to a Stream (dataToStream is a hypothetical function that do this conversion): val dataStream: Stream[T] = dataToStream(data) I suppose this problem could be resolved by continuations: // let's assume that we don't know how data is implemented // we just know that it generates integers def data(block: Int => Unit) { for (i <- 0 to 10) block(i) } // here we can print all data integers data { i => println(i) } // >> but what we really

es6的next()理解

こ雲淡風輕ζ 提交于 2019-11-30 00:03:32
看下这个简单的例子,弄懂了就知道next的执行流程了,偶尔得让脑子运动一下下 总结的简单流程是: (1) 执行next后会从上往下一次返回每个yield表达式的值, (2) 如果next有传参的话,会整个覆盖掉将要返回当前yield的上一个yield, 所以它的流程才会是: 1(无论是否传参,传什么参数,返回第一个表达式的值1) 3(返回第二个表达式的值yield a,a=上一个yield表达式yield 1,被参数3覆盖,所以a=3) 4(返回第三个表达式的值yield b,b=上一个yield表达式yield a,被参数4覆盖,所以b=4) 12(返回return a+b+c,c=上一个yield表达式yield b,被参数5覆盖,所以c=5,也就是a+b+c=3+4+5=12) 这下没错了, 太不容易了 来源: https://www.cnblogs.com/lightmusic/p/11541061.html