yield

What are real life applications of yield?

I know what yield does, and I've seen a few examples, but I can't think of real life applications, have you used it to solve some specific problem? (Ideally some problem that cannot be solved some other way) ...

What is the yield keyword used for in C#?

In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet: IEnumerable<object> FilteredList() { foreach( object item in FullList ) { if( IsItemInPartialList( item ) yield return item; } } What does the yield keyword do there? I've seen it referenced i...

Using yield to iterate over a datareader might not close the connection?

Here is a sample code to retrieve data from a database using the yield keyword that I found in a few place while googling around : public IEnumerable<object> ExecuteSelect(string commandText) { using (IDbConnection connection = CreateConnection()) { using (IDbCommand cmd = CreateCommand(commandText, connection)) ...

Rails check if yield :area is defined in content_for

I want to do a conditional rendering at the layout level based on the actual template has defined content_for(:an__area), any idea how to get this done? ...

The Python yield keyword explained

What is the use of the yield keyword in Python? What does it do? For example, I'm trying to understand this code (**): def node._get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: yield self._leftchild if self._rightchild and distance + max_dist >= self._...

What does "yield break;" do in C#?

I have seen this syntax in MSDN, but I don't know what it does. Does anyone know? ...

Iterator Pattern VB.net (C# would use yield!)

Hello, I want to implement the iterator pattern in VB.net, which does not have the yield keyword. Any ideas or links please? ...

Is Yield Return == IEnumerable & IEnumerator?

I just want to verify, is yield return a shortcut for implementing IEnumerable and IEnumerator? Thanks, John ...

Concurrency or Performance Benefits of yield return over returning a list

I was wondering if there is any concurrency (now or future), or performance benefit to using yield return over returning a list. See the following examples Processing Method void Page_Load() { foreach(var item in GetPostedItems()) Process(item); } using yield return IEnumerable<string> GetPostedItems() { yield return Item1....

Some help understanding "yield"

In my everlasting quest to suck less I keep bumping into the "yield" statement. I have tried to wrap my head around it a few times now, but I'm stumped every time with the same error. It goes a little something like this: The body of [someMethod] cannot be an iterator block because 'System.Collections.Generic.List< AClass>' is not...

Is yield useful outside of LINQ?

When ever I think I can use the yield keyword, I take a step back and look at how it will impact my project. I always end up returning a collection instead of yeilding because I feel the overhead of maintaining the state of the yeilding method doesn't buy me much. In almost all cases where I am returning a collection I feel that 90% of t...

Yield keyword value added?

still trying to find where i would use the "yield" keyword in a real situation. I see this thread on the subject http://stackoverflow.com/questions/39476/what-is-the-yield-keyword-used-for-in-c but in the accepted answer, they have this as an example where someone is iterating around Integers() public IEnumerable<int> Integers() { y...

How to iterate through a tree structure using a generator?

Hello people. I'm trying to figure out how to implement a function in a tree node which returns all its descendant leaves (whether direct or indirect). However, I don't want to pass a container in which the leaf nodes will be put recursively (the tree could be huge), instead I'd like to use a generator to iterate through the tree. I've ...

CCR, Yield and VB.net

I've been trying to get my head around the CCR (Concurrency And Coordination Runtime) to see if it is worth learning. I program mostly in Vb.net and in most of the examples of using the CCR use the Yield keyword. How essential is Yield for using the CCR? Are there workarounds? Will VB.net Get the Yield command? (If Not WHY NOT?) ...

Garbage collection in yield Methods

Say I have a method like this (stolen from a previous SO answer by Jon Skeet): public static IEnumerable<TSource> DuplicatesBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> seenKeys = new HashSet<TKey>(); foreach (TSource element in source) { // Yield it if the...

What's the use of yield break?

Can anyone see a use for the "yield break" statement that could not have been otherwise achieved by using "break" or "return". This statement seems to be utterly useless. What's more, without this statement, the "yield return X" statement could have been simplified to "yield X", which much more readable. What am I missing? ...

Overloaded use of yield return

I don't have much experience with using the yield keyword. I have these IEnumerable<T> extensions for Type Conversion. My question is does the first overloaded method have the same yield return effect that I'm getting from the second method? public static IEnumerable<TTo> ConvertFrom<TTo, TFrom>(this IEnumerable<TFrom> toList) { r...

How can I make my yielded IEnumerable work with PagedList

I'm using Troy Goode's paged List in my project. Normally you just feed it an IEnumerable, a startindex and an item count and it all works. Now however I'm trying to feed it an IEnumerable I generate as follows: private static IEnumerable<Color> GetColors(Query query) { IndexSearcher searcher = new IndexSearcher(luceneIndexpath); Hits...

Using for...else in Python generator expressions

I'm a big fan of Python's for...else syntax - it's surprising how often it's applicable, and how effectively it can simplify code. However, I've not figured out a nice way to use it in a generator expression, for example: def iterate(i): for value in i: yield value else: print 'i is empty' In the above example...

Creating lists using yield in Ruby and Python

I'm trying to come up with an elegant way of creating a list from a function that yields values in both Python and Ruby. In Python: def foo(x): for i in range(x): if bar(i): yield i result = list(foo(100)) In Ruby: def foo(x) x.times {|i| yield i if bar(i)} end result = [] foo(100) {|x| result << x} Although I love ...