yield

What's the yield keyword in javascript?

I heard about a "yield" keyword in javascript, but i found very poor documentation about it. Can someone explain me (or recommend a site that explains) its usage and what it is used for? ...

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

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 specifi...

Javascript check yield support

Hi, i read about the yield keyword in javascript and i need to use it in my project. I read that this keyword has been implemented starting from a certain version of JS so i think that old browsers don't support it (right?). Is there a way to check if the yield keyword is supported? or at least is there a way to check if the version of ...

cx_Oracle, generators, and threads in Python

Hi all, What is the behavior of cx_Oracle cursors when the connection object is used by different threads? How would generators affect this behavior? Specifically... Edit: The original example function was incorrect; a generator was being returned by a sub function, yield wasn't used directly in the loop. This clarifies when finally...

Is there such a thing as "too many yield statements" in python?

If doing a directory listing and reading the files within, at what point does the performance of yield start to deteriorate, compared to returning a list of all the files in the directory? Here I'm assuming one has enough RAM to return the (potentially huge) list. PS I'm having problems inlining code in a comment, so I'll put some exam...

Workaround for python 2.4's yield not allowed in try block with finally clause

I'm stuck on python2.4, so I can't use a finally clause with generators or yield. Is there any way to work around this? I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've thought of (mainly involving __del__ and trying to make sure it runs within a reasonable tim...

PLINQ delayed execution

I'm trying to understand how parallelism might work using PLINQ, given delayed execution. Here is a simple example. string[] words = { "believe", "receipt", "relief", "field" }; bool result = words.AsParallel().Any(w => w.Contains("ei")); With LINQ, I would expect the execution to reach the "receipt" value and return true, without ex...

C#: yield return within a foreach fails - body cannot be an iterator block

Consider this bit of obfuscated code. The intention is to create a new object on the fly via the anonymous constructor and yield return it. The goal is to avoid having to maintain a local collection just to simply return it. public static List<DesktopComputer> BuildComputerAssets() { List<string> idTags = GetComputerIdTag...

Passing multiple codeblocks as arguments

I have a method which takes a code block. def opportunity @opportunities += 1 if yield @performances +=1 end end and I call it like this: opportunity { @some_array.empty? } But how do I pass it more than one code block so that I could use yield twice, something like this: def opportunity if yield_1 ...

How to Pythonically yield all values from a list?

Suppose I have a list that I wish not to return but to yield values from. What is the most Pythonic way to do that? Here is what I mean. Thanks to some non-lazy computation I have computed the list ['a', 'b', 'c', 'd'], but my code through the project uses lazy computation, so I'd like to yield values from my function instead of returni...

C# yield return not returning an item as expected

I have following code: private void ProcessQueue() { foreach (MessageQueueItem item in GetNextQueuedItem()) PerformAction(item); } private IEnumerable<MessageQueueItem> GetNextQueuedItem() { if (_messageQueue.Count > 0) yield return _messageQueue.Dequeue(); } Initially there is one item in the queue as Process...

C# yield in nested method

If I step through the following code the call to ReturnOne() is skipped. static IEnumerable<int> OneThroughFive() { ReturnOne(); yield return 2; yield return 3; yield return 4; yield return 5; } static IEnumerator<int> ReturnOne() { yield return 1; } I can only assume the compiler is stripping it out because ...

Is there a way to implement Caliburn-like co-routines in VB.NET since there's no yield keyword

Note that I'm aware of other yield in vb.net questions here on SO. I'm playing around with Caliburn lately. Bunch of great stuff there, including co-routines implementation. Most of the work I'm doing is C# based, but now I'm also creating an architecture guideline for a VB.NET only shop, based on Rob's small MVVM framework. Everythin...

Attribute lost with yield

There is some code that I'm trying to convert from IList to IEnumerable: [Something(123)] public IEnumerable<Foo> GetAllFoos() { SetupSomething(); DataReader dr = RunSomething(); while (dr.Read()) { yield return Factory.Create(dr); } } The problem is, SetupSomething() comes from the base class and uses: Attribute.GetCu...

Rails 3 - yield return or callback won't call in view <%= yield(:sidebar) || render('shared/sidebar') %>

Hey folks, I'm migrating a Website from Rails 2 (latest) to Rails 3 (beta2). Testing with Ruby 1.9.1p378 and Ruby 1.9.2dev (2010-04-05 trunk 27225) Stuck in a situation, i don't know which part will work well. Suspect yield is the problem, but don't know exactly. In my Layout Files I use the following technique quite often: app/views...

Implementing "Generator" support in a custom language

I've got a bit of fettish for language design and I'm currently playing around with my own hobby language. (http://rogeralsing.com/2010/04/14/playing-with-plastic/) One thing that really makes my mind bleed is "generators" and the "yield" keyword. I know C# uses AST transformation to transform enumerator methods into statemachines. But...

Multithreading, when to yield versus sleep

hello. To clarify terminology, yield is when thread gives up its time slice. My platform of interest is POSIX threads, but I think 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 eith...

When should I use Yield in c#?

I have a vage understanding of the Yield keyword in c#, but I haven't yet seen the need to use it in my code. This probably comes from a lack of understanding of it.. So; What are some typical good usages of Yield? ...

Is thread-safe to use the 'yield' operator inside an extension method'?

Would be thread-safe to use the yield operator inside an extension method? For example: public static IEnumerable<CartItem> GetItems( this Cart cart ) { { while( cart.hasNext() ) yield return cart.GetNextItem( ); } } ...

What is the best way to translate this recursive python method into Java?

In another question I was provided with a great answer involving generating certain sets for the Chinese Postman Problem. The answer provided was: def get_pairs(s): if not s: yield [] else: i = min(s) for j in s - set([i]): for r in get_pairs(s - set([i, j])): yield [(i, j)] + r for x ...