yield

In C#, why can't an anonymous method contain a yield statement?

无人久伴 提交于 2019-11-27 10:35:39
I thought it would be nice to do something like this (with the lambda doing a yield return): public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() { IList<T> list = GetList<T>(); var fun = expression.Compile(); var items = () => { foreach (var item in list) if (fun.Invoke(item)) yield return item; // This is not allowed by C# } return items.ToList(); } However, I found out that I can't use yield in anonymous method. I'm wondering why. The yield docs just say it is not allowed. Since it wasn't allowed I just created List and added the items to it. Eric Lippert

Can someone demystify the yield keyword?

六眼飞鱼酱① 提交于 2019-11-27 10:31:50
问题 I have seen the yield keyword being used quite a lot on Stack Overflow and blogs. I don't use LINQ. Can someone explain the yield keyword? I know that similar questions exist. But none really explain what is its use in plain simple language. 回答1: By far the best explanation of this (that I've seen) is Jon Skeet's book - and that chapter is free! Chapter 6, C# in Depth. There is nothing I can add here that isn't covered. Then buy the book; you will be a better C# programmer for it. Q: Why didn

Ruby: Proc#call vs yield

☆樱花仙子☆ 提交于 2019-11-27 10:25:31
What are the behavioural differences between the following two implementations in Ruby of the thrice method? module WithYield def self.thrice 3.times { yield } # yield to the implicit block argument end end module WithProcCall def self.thrice(&block) # & converts implicit block to an explicit, named Proc 3.times { block.call } # invoke Proc#call end end WithYield::thrice { puts "Hello world" } WithProcCall::thrice { puts "Hello world" } By "behavioural differences" I include error handling, performance, tool support, etc. I think the first one is actually a syntactic sugar of the other. In

WWW/UnityWebRequest POST/GET request won't return the latest data from server/url

梦想与她 提交于 2019-11-27 09:32:21
I am creating a HoloLens app using Unity which has to take data from a REST API and display it. I am currently using WWW datatype to get the data and yield return statement in a coroutine that will be called from the Update() function. When I try to run the code, I get the latest data from the API but when someone pushes any new data onto the API, it does not automatically get the latest data in real time and I have to restart the app to see the latest data. My Code: using UnityEngine; using UnityEngine.UI; using System.Collections; using System; using Newtonsoft.Json; using System.Collections

Is there a way to efficiently yield every file in a directory containing millions of files?

佐手、 提交于 2019-11-27 09:05:14
I'm aware of os.listdir , but as far as I can gather, that gets all the filenames in a directory into memory, and then returns the list. What I want, is a way to yield a filename, work on it, and then yield the next one, without reading them all into memory. Is there any way to do this? I worry about the case where filenames change, new files are added, and files are deleted using such a method. Some iterators prevent you from modifying the collection during iteration, essentially by taking a snapshot of the state of the collection at the beginning, and comparing that state on each move

SyntaxError: Unexpected Identifier (Generators in ES6)

我与影子孤独终老i 提交于 2019-11-27 08:56:45
I came up with this simple experiment after reading the documentation on generators from MDN : var nodes = { type: 'root', value: [ { type: 'char', value: 'a' }, { type: 'char', value: 'b' }, { type: 'char', value: 'c' }, ], }; function* recursiveGenerator(node) { if (node.type === 'root') { node.value.forEach(function (subnode) { for (var suffix of recursiveGenerator(subnode)) { yield suffix; } }); } else { yield node.value; } } for (generated of recursiveGenerator(nodes)) { console.log(generated); } Running it on node.js v0.11.9 with the --harmony flag set produces the following error: alix

Trouble yielding inside a block/lambda

橙三吉。 提交于 2019-11-27 07:43:37
问题 I have the following Ruby code: # func1 generates a sequence of items derived from x # func2 does something with the items generated by func1 def test(x, func1, func2) func1.call(x) do | y | func2.call(y) end end func1 = lambda do | x | for i in 1 .. 5 yield x * i end end func2 = lambda do | y | puts y end test(2, func1, func2) # Should print '2', '4', '6', '8', and '10' This does not work, of course. test.rb:11: no block given (LocalJumpError) from test.rb:10:in `each' from test.rb:10 from

概念术语

我的梦境 提交于 2019-11-27 06:10:10
1. 格式化输出:利用format "{1} {0} {1}".format("hello", "world") # 设置指定位置 'world hello world' 或 %s,%d % str1,int1 * format的四种玩法(python推荐使用format做格式化输出) ```python # 第一种 按位置占位 跟%s原理一致 str1 = 'my name is {}, my age is {}'.format(18,'age') print(str1) # 第二种 按索引占位 str1 = 'my name is {1}, my age is {0}'.format('egon',18) print(str1) # 第三种 指名道姓占位(关键字传参) str1 = 'my name is {name}, my age is {age}'.format(name='egon',age=18) print(str1) #第四种 format简写,两个变量进行拼接时用 name='egon' age=18 str1 = f'my name is {name}, my age is {age}' print(str1) ``` 2. 形参 实参 位置参数 关键字参数 默认参数 形参: def func(arg1,arg2) arg1,arg2即为形参 实参

Day14之列表推导式到匿名函数

爷,独闯天下 提交于 2019-11-27 06:09:57
今天早上六点多起来妙妙,然后继续睡,眼睛一闭一睁八点零六,炸毛了,弹射起步,十分钟全部搞定,滑滑板来到教室,太赶了,让我一天的状态都是懵的,这种感觉真的太难受了,困,不必说,站起来也听不进去,这导致了我今天的听课效率比国家人均GDP都低。然后老师讲课的时候会问,这个听懂的举手 即使我没听懂,我还是举手了 根本没跟上啊。。。。 好了 今天的打气流程 。。。。。 今日洗脑金句:加油,最后悔的事不是失败,而是“你本可以”。 三元表达式 dog_name = 'crazy_dog1' print('疯狂舔🐕') if dog_name == 'crazy_dog' else print('666') 这个是什么意思,其实和原来的逻辑判断一样,只不过,这个逼格更高,但是不推荐,因为用的人少,而且大家都看不懂,公司就会把你开了,找一个写代码让人看的懂的程序员来。 那么这句的意思是什么,就是如果 dog_name == ‘crazy_dog' ,就打印疯狂舔狗,不然的话就打印666。 不过你们可以别听我的,尽管去用,要知道,你写的代码如果只有你自己看的懂,你就是公司不可或缺的员工,如果你的bug很多,不停地改,公司就会觉得你很牛逼,很能改bug。 所以放手去做吧,工作可以再找,被磨平的棱角一去不复返! 列表推导式 别去管这个原理,根本就是找罪受。没必要,也不用。 lt = [] for i in

Equivalent C++ to Python generator pattern

久未见 提交于 2019-11-27 06:02:45
I've got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some manner. Python This is a basic sequence generator, clearly too large to store a materialized version. def pair_sequence(): for i in range(2**32): for j in range(2**32): yield (i, j) The goal is to maintain two instances of the sequence above, and iterate over them in semi-lockstep, but in chunks. In the example below the first_pass uses the sequence of