yield

F#: Why is using a sequence so much slower than using a list in this example

Background: I have a sequence of contiguous, time-stamped data. The data-sequence has holes in it, some large, others just a single missing value. Whenever the hole is just a single missing value, I want to patch the holes using a dummy-value (larger holes will be ignored). I would like to use lazy generation of the patched sequence, an...

How do I select a subset of items from an anonymous type IEnumerable?

I have the following code. MyDataContext db = MyDataContext.Create(); bc = db.BenefitCodes.Select( b => new { BenCd = b.BenCd , Description = b.BenDesc , BenInter...

Can someone demystify the yield keyword?

I have seen the yield keyword being used quite a lot on Stack Overflow and blogs. I don't use LINQ. Can someone explain the yield keyword? I know that similar questions exist. But none really explain what is its use in plain simple language. ...

Is it better to yield return lookup lists or preload a static list in c#?

I have a simple lookup list that I will use to populate a dropdown in Silverlight. In this example I'm using US States. I'm trying to figure out if its better to return a static list or use the yield keyword. Of the following two pieces of code, which is the preferred and why? Version 1: Using yield return public class States { ...

Is yield return in C# thread-safe?

I have the following piece of code: private Dictionary<object, object> items = new Dictionary<object, object>; public IEnumerable<object> Keys { get { foreach (object key in items.Keys) { yield return key; } } } Is this thread-safe? If not do I have to put a lock around the loop or the y...

Do I need to parenthesise yield in Ruby?

In Ruby I can use result << (yield element) and everything works, but if I do result.push(yield element) I get a warning about needing parentheses for future compatibility. I can change the above to result.push(yield(element)) and the interpreter is happy again, but I don't understand why I need parentheses in one call to yield ...

What is yield return in C#?

Possible Duplicate: What is the yield keyword used for in C#? I've recently noticed the "yield" keyword and it caught my attention. English is not my primary language so the meaning of the word itself may elude me as well. What does that keyword mean in C# and how is it used? ...

Ruby: Proc#call vs yield

What are the behavioural differences between the following two implementations in Ruby of the thrice method? module WithYield def self.thrice 3.times { yield } # yield to the implicit block argument end end module WithProcCall def self.thrice(&block) # & converts implicit block to an explicit, named Proc 3.times { b...

yield: why can't i write: p test_method { i.upcase }

def test_method ["a", "b", "c"].map {|i| yield(i) } end If I call test_method like this: p test_method {|i| i.upcase } # => ["A", "B", "C"] Why do I need the {|i|} inside the block, instead of just saying this: p test_method { i.upcase } The reason I think so is because when yield is called in test_method, we already have an {|...

Is it necessary to refer to self in: yield(self[i])

In this example from a blog post, class Array def each i = 0 while(i < self.length) do yield(self[i]) i += 1 end end end my_array = ["a", "b", "c"] my_array.each {|letter| puts letter } # => "a" # => "b" # => "c" Is it necessary to use self in the statement: yield(self[i]) Or would it be ok to simply sa...

yield break; - crazy behaviour

using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication4 { class Program { static void Main (string[] args) { var test1 = Test1(1, 2); var test2 = Test2(3, 4); } static IEnumerable Test1(int v1, int v2) ...

yield return versus return select

Which are the advantages/drawbacks of both approaches? return items.Select(item => DoSomething(item)); versus foreach(var item in items) { yield return DoSomething(item); } EDIT As they are MSIL roughly equivalent, which one you find more readable? ...

Caching IEnumerable

public IEnumerable<ModuleData> ListModules() { foreach (XElement m in Source.Descendants("Module")) { yield return new ModuleData(m.Element("ModuleID").Value); } } Initially the above code is great since there is no need to evaluate the entire collection if it is not needed. However, once all the Modules have been ...

Iterating over nested lists with a Next() function, without a generator.

Whilst I'd love to solve this problem in python, I'm stuck in Delphi for this one. I have nested lists (actually objects with nested lists as properties, but nevermind), and I want to iterate over them in a generator fashion. That is, I want to write a Next function, which gives me the next item from the leaves of the tree described by t...

return column values as IEnumerable

I have this code working: public IEnumerable<string> GetEmpNames() { var cmd = SqlCommand("select [EmpName] from [dbo].[Emp]"); using (var rdr = cmd.ExecuteReader()) while (rdr.Read()) yield return (string) rdr["EmpName"]; } However, I'm wondering if there's a better (LINQish) way, not having to resort to y...

Does Scala have an equivalent to C# yield?

I'm new to Scala, and from what I understand yield in Scala is not like yield in C#, it is more like select. Does Scala have something similar to C#'s yield? C#'s yield is great because it makes writing iterators very easy. Update: here's a pseudo code example from C# I'd like to be able to implement in Scala: public class Graph<T> { ...

What is the difference (if any) between "yield" and "yield return" in C#?

I've traditionally used yield in C# without the return, e.g.: IEnumerable<T> Foobar() { foreach( var foo in _stuff ) { yield foo; } } But in other examples I've seen it written as "yield return foo;", see: http://msdn.microsoft.com/en-us/library/9k7k7cf0%28VS.80%29.aspx. Is there any difference? ...

How can I traverse a file system with a generator?

I'm trying to create a utility class for traversing all the files in a directory, including those within subdirectories and sub-subdirectories. I tried to use a generator because generators are cool; however, I hit a snag. def grab_files(directory): for name in os.listdir(directory): full_path = os.path.join(directory, name...

Looking for a faster implementation for IEnumerable/IEnumerator

I'm trying to optimize a concurrent collection that tries to minimize lock contention for reads. First pass was using a linked list, which allowed me to only lock on writes while many simultaneous reads could continue unblocked. This used a custom IEnumerator to yield the next link value. Once i started comparing iteration over the colle...

C# 2.0 - Is there any way to do a `GroupBy` with a yielded itterator block?

I'm working with a C# 2.0 app so linq/lambda answers will be no help here. Basically I'm faced with a situation where i need to yield return an object but only if one if it's properties is unique (Group By). For example,..say i have a collection of users and i want a grouped collection based on name (i might have 20 Daves but I'd only ...