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)
...
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...
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))
...
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?
...
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._...
I have seen this syntax in MSDN, but I don't know what it does. Does anyone know?
...
Hello,
I want to implement the iterator pattern in VB.net, which does not have the yield keyword.
Any ideas or links please?
...
I just want to verify, is yield return a shortcut for implementing IEnumerable and IEnumerator?
Thanks,
John
...
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....
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...
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...
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...
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 ...
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?)
...
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...
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?
...
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...
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...
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...
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 ...