yield

Why do Python yield statements form a closure?

匆匆过客 提交于 2019-12-02 20:24:56
I have two functions that return a list of functions. The functions take in a number x and add i to it. i is an integer increasing from 0-9. def test_without_closure(): return [lambda x: x+i for i in range(10)] def test_with_yield(): for i in range(10): yield lambda x: x+i I would expect test_without_closure to return a list of 10 functions that each add 9 to x since i 's value is 9 . print sum(t(1) for t in test_without_closure()) # prints 100 I expected that test_with_yield would also have the same behavior, but it correctly creates the 10 functions. print sum(t(1) for t in test_with_yield()

Does the C# Yield free a lock?

此生再无相见时 提交于 2019-12-02 20:07:11
I have the following method: public static IEnumerable<Dictionary<string, object>> GetRowsIter (this SqlCeResultSet resultSet) { // Make sure we don't multi thread the database. lock (Database) { if (resultSet.HasRows) { resultSet.Read(); do { var resultList = new Dictionary<string, object>(); for (int i = 0; i < resultSet.FieldCount; i++) { var value = resultSet.GetValue(i); resultList.Add(resultSet.GetName(i), value == DBNull.Value ? null : value); } yield return resultList; } while (resultSet.Read()); } yield break; } I just added the lock(Database) to try and get rid of some concurancy

yield方法

血红的双手。 提交于 2019-12-02 20:02:01
jdk方法定义 public static native void yield(); yield()方法的作用是 放弃当前的CPU资源,将它让给其它任务。但放弃的时间不确定。 | | jvm层实现 jvm_yield 1 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass)) 2 JVMWrapper("JVM_Yield"); 3 //检查是否设置了DontYieldALot参数,默认为fasle 4 //如果设置为true,直接返回 5 if (os::dont_yield()) return; 6 //如果ConvertYieldToSleep=true(默认为false),调用os::sleep,否则调用os::yield 7 if (ConvertYieldToSleep) { 8 os::sleep(thread, MinSleepInterval, false);//sleep 1ms 9 } else { 10 os::yield(); 11 } 12 JVM_END ConvertYieldToSleep(默认false)如果true: 调用系统sleep函数 1ms 如果false:调用内核系统的os::yield()|| 1 //sched_yield是linux kernel提供的API

Multithreading, when to yield versus sleep

不羁岁月 提交于 2019-12-02 19:31:39
To clarify terminology, yield is when thread gives up its time slice. My platform of interest is POSIX threads, but I think the question is general. Suppose I have consumer/producer pattern. If I want to throttle either consumer or producer, which is better to use, sleep or yield? I am mostly interested in efficiency of using either function. The "right" way to code a producer / consumer is to have the consumer wait for the producer's data. You can achieve this by using a synchronization object such as a Mutex. The consumer will Wait on the mutex, which blocks it from executing until data is

What is the preferred way to implement 'yield' in Scala?

情到浓时终转凉″ 提交于 2019-12-02 18:39:59
I am doing writing code for PhD research and starting to use Scala. I often have to do text processing. I am used to Python, whose 'yield' statement is extremely useful for implementing complex iterators over large, often irregularly structured text files. Similar constructs exist in other languages (e.g. C#), for good reason. Yes I know there have been previous threads on this. But they look like hacked-up (or at least badly explained) solutions that don't clearly work well and often have unclear limitations. I would like to write code something like this: import generator._ def yield_values

Can I yield from an inner function?

流过昼夜 提交于 2019-12-02 18:16:01
With ES6 generators, I see code like this: var trivialGenerator = function *(array) { var i,item; for(var i=0; i < array.length; i++){ item = array[i]; yield item; }; }; Is it possible to write something more like the code below instead? var trivialGenerator = function *(array) { array.forEach(function *(item){ yield item; }); }; I'm asking because the classic for loop is an abomination. alexpods No, you can't use yield inside of the inner function. But in your case you don't need it. You can always use for-of loop instead of forEach method. It will look much prettier, and you can use continue

Is Yield Return == IEnumerable & IEnumerator?

痞子三分冷 提交于 2019-12-02 17:57:25
Is yield return a shortcut for implementing IEnumerable and IEnumerator ? Yes, it is. You can find out a lot more about it in chapter 6 of my book, C# in Depth. Fortunately chapter 6 is available for free from Manning's web site . I also have two other articles on the book's web site. Feedback welcome. To add to the link-fest, Raymond Chen did a nice little series on C# iterators a few months ago: http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx http://blogs.msdn.com/oldnewthing/archive/2008/08/14/8862242.aspx

Is it possible to use 'yield' to generate 'Iterator' instead of a list in Scala?

放肆的年华 提交于 2019-12-02 17:41:08
Is it possible to use yield as an iterator without evaluation of every value? It is a common task when it is easy to implement complex list generation, and then you need to convert it into Iterator , because you don't need some results... Sure. Actually, there are three options for non-strictness, which I list below. For the examples, assume: val list = List.range(1, 10) def compute(n: Int) = { println("Computing "+n) n * 2 } Stream . A Stream is a lazily evaluated list. It will compute values on demand, but it will not recompute values once they have been computed. It is most useful if you'll

How does this class implement the “__iter__” method without implementing “next”?

早过忘川 提交于 2019-12-02 17:13:29
I have the following code in django.template: class Template(object): def __init__(self, template_string, origin=None, name='<Unknown Template>'): try: template_string = smart_unicode(template_string) except UnicodeDecodeError: raise TemplateEncodingError("Templates can only be constructed from unicode or UTF-8 strings.") if settings.TEMPLATE_DEBUG and origin is None: origin = StringOrigin(template_string) self.nodelist = compile_string(template_string, origin) self.name = name def __iter__(self): for node in self.nodelist: for subnode in node: yield subnode def render(self, context): "Display

yield break in Python

北城余情 提交于 2019-12-02 16:58:06
according to answer to this question , yield break in C# is equivalent to return in python. in normal case, 'return' indeed stop a generator. But if your function does nothing but return, you will get a None not an empty iterator, which is returned by yield break in C# def generate_nothing(): return for i in generate_nothing(): print i you will get a TypeError: 'NoneType' object is not iterable. but if I add an never run yield before return, this function return what I expect. def generate_nothing(): if False: yield None return if works but seems wired. Who has better idea? thanks, Sam Simmons