yield

Rewrite this foreach yield to a linq yield?

Say I have the following code (context narrowed down as to keep the question scope limited) public static IEnumerable<Color> GetThemColors(){ var ids = GetThePrimaryIds(); foreach (int id in ids){ yield return GetColorById(id); } ids = GetTheOtherIds(); foreach (int id in ids){ yield return GetOtherColorsById(id); } } I woul...

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

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

Pitfalls of (Mis)Using C# Iterators to Implement Coroutines

I am writing refactoring a Silverlight program to consumes a portion of its existing business logic from a WCF service. In doing so, I've run into the restriction in Silverlight 3 that only allows asynchronous calls to WCF services to avoid cases where long-running or non-responsive service calls block the UI thread (SL has an interestin...

In there something similar to Java's Thread.yield() in Python? Does that even make sense?

I want to tell my Python threads to yield, and so avoid hogging the CPU unnecessarily. In Java, you could do that using the Thread.yield() function. I don't think there is something similar in Python, so I have been using time.sleep(t) where t = 0.00001. For t=0 there seems to be no effect. I think that maybe there is something I am not...

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

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

Interesting use of the C# yield keyword in Nerd Dinner tutorial

Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this snippet of code: public IEnumerable<RuleViolation> GetRuleViolations() { if (String.IsNullOrEmpty(Title)) yield return new RuleViolation("Title required", "Title"); if (String.IsNullOrEmpty(Description)) yield return new RuleV...

C# - Yield gives an unusable type

I have a class and a set of IEnumerables that are using this class to give me a list in a list. (See this question's answer for details.) Here is the code: public IEnumerable<IEnumerable<WorkItemColumn>> EnumerateResultSet(WorkItemCollection queryResults)//DisplayFieldList displayFieldList) { foreach (WorkItem workItem in queryRes...

Optimize Code by using yield or changing algorithm

The code below works but I want to optimize the code using yield or by changing algorithm. public IEnumerable<Book> GetAuthorWithBookName(string keyword) { var bookAndAuthorList = new List<Book>(); List<Author> AuthorNameList = getAuthorName(keyword); foreach (var author in AuthorNameList) { XmlNode booksNames = ...

Is there a Java equivalent to C#'s 'yield' keyword?

I know there is no direct equivalent in Java itself, but perhaps a third party? It is really convenient. Currently I'd like to implement an iterator that yields all nodes in a tree, which is about five lines of code with yield. ...

Python: generator expression vs. yield

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) f...

what does yield as assignment do? myVar = (yield)

I'm familiar with yeild to return a value thanks mostly to this question but what does yield do when it is on the right side of an assignment? @coroutine def protocol(target=None): while True: c = (yield) def coroutine(func): def start(*args,**kwargs): cr = func(*args,**kwargs) cr.next() return c...

Rails: using "content_for" after the corresponding "yield" inside layout

Hi everybody, I think this has been asked before but even though I searched Google I haven't come up with a solution. So this is what I'm trying to do in Rails 2.3.5: layouts/application.html.erb: <html> <head> ... some other stuff <%= yield :head %> </head> <body> <% content_for :head, "something that belongs in the...

Different results from yield vs return

I don't really understand how yield statement works in this situation. The problem says that given an expression without parentheses, write a function to generate all possible fully parenthesized (FP) expressions. Say, the input is '1+2+3+4' which should be generated to 5 FP expressions: (1+(2+(3+4))) (1+((2+3)+4)) ((1+2)+(3+4)) ((1+(2...

IEnumerable and Recursion using yield return

Hi everyone, I have an IEnumerable<T> method that I'm using to find controls in a WebForms page. The method is recursive and I'm having some problems returning the type I want when the yield return is returnig the value of the recursive call. My code looks as follows: public static IEnumerable<Control> ...

Recursive silverlight Element finder extension method

I need an extension method to traverse all Textboxes on my Silverlight page. Assume I always use a grid Layout, then I have: public static IEnumerable<UIElement> Traverse(this UIElementCollection source, Func<Grid, UIElementCollection> fnRecurse) { foreach (UIElement item in source) { yield return item; ...

Implementing yield (yield return) using Scala continuations

How might one implement C# yield return using Scala continuations? I'd like to be able to write Scala Iterators in the same style. A stab is in the comments on this Scala news post, but it doesn't work (tried using the Scala 2.8.0 beta). Answers in a related question suggest this is possible, but although I've been playing with delimited...

Yield multiple objects at a time from an iterable object?

How can I yield multiple items at a time from an iterable object? For example, with a sequence of arbitrary length, how can I iterate through the items in the sequence, in groups of X consecutive items per iteration? (Question inspired by an answer which used this technique.) ...

Is there any way to make a yield created Iterator Continue to the next item upon an Exception?

Hello Is there any way to have a yield created iterator continue to the next item when an exception occurs inside one of the iterator blocks? This is currently not working: Boolean result; while (true) { try { result = enumerator.MoveNext(); //Taken from a yield created e...

Yield the shortest path in state transistions

I am trying to come up with an algorithm for a tree traversal, but I am getting stuck. This is a fairly hard question (compared to others I have asked) so I may need to keep figuring on my own. But I thought I would throw it out here. I have the following class structure: public class Transition { // The state we are moving from....

Rewrite simple ruby function to use a block

I dont know the correct terminology for what i am asking I tried to google it and couldnt ind anything for the same reason I am writing a ruby library, and i want to rewite the functions so they work as below as i prefer it for readability (inside a block?) at the moment i have a function that does this @dwg = Dwg.new("test.dwg") @dwg...