Skip to main content

C# Wish List: yield for Collections

I remembered a new C# feature I want today. If you're familiar with 'yield', it's a great keyword in C# used in iterator blocks. eg:

 // Ignoring that there would be much simpler ways to do this anyway.  
 public static IEnumerable<int> GetEvens(IEnumerable<int> numbers)  
   foreach (var number in numbers)  
     if (number % 2 == 0)  
       yield return number;  

Using yield will also result in lazy evaluation as demonstrated here, when using the method above:

 static void Main(string[] args)  
   var list = Enumerable.Range(1, 10).ToList();  
   var output = GetEvens(list);  
   foreach (var item in output)  

Which prints:


Which is an awesome feature. However, I often find cases where I'd like to yield the rest of a collection. For instance, in the Combine method of a MergeSort I was implementing, I had this block from where I wanted to get the rest of the right collection (because the left collection was now empty):

 if (leftIndex == left.Count)  
   foreach (var x in right.Skip(rightIndex))  
     yield return x;  

But it would be much nicer if I could yield the collection. In F# you can yield a collection with yield! eg:

 let FooOneToTen =   
   seq { yield! [1..10] }  

Which I suppose in C# would be more like this:

 if (leftIndex == left.Count)  
   yield! return right.Skip(rightIndex);  

That would improve readability, but would understandably be a very low priority feature.


Popular posts from this blog

Constructing a Trie in F#

This post might get a bit more context next week, but essentially, in continuation of looking at data structures in F# and C# I picked prefix trees (tries) as the next step. A trie is similar to a BST except the search is cumulative. A simple example would be building a spellchecker. The spell checker can use the structure to see if a word exists quickly and efficiently.

It works by creating a new node for each part of the word, using existing nodes if they're already in place. You can see in this diagram an example structure after an insertion of several words.

In the above diagram the words ANT, AND, BATS have been added into the data structure. You'll notice that the word BATS can also be the word BAT and that becomes a part of the structure, setting a value at each node to state if this is a place that a word terminates.

1 2 3 4// Node type for storing a trie. typeTrieNode=|Nodeofchar*TrieNodelist*bool|RootofTrieNodelist
I defined the type to have 2 options. The root node,…

A Docker Experiment

Containers are a topic that have been rising in occurrence for me, for the last year or so. From using them as part of the architecture at work or for various pet projects friends have been working on. I figured it was time to experiment myself and get to grips with what people have been talking about. It seemed like a good idea to find some introductory material that would give me an overview of its uses without delving too far into the details so I could see what it actually was, so I found a PluralSight course about Docker, specifically “Docker and Containers:The Big Picture”. This gave a nice overview of what Docker is, and more importantly what it was trying to achieve and how to use it. With a bit more of an understanding, I wanted to use it for something, preferably something familiar. I decided to try setting up ElasticSearch and Kibana containers, where Kibana would visualize the ElasticSearch data. I used bits of this article along the way as a guide, if you'd prefer a more…

Self-Producing an EP Pt.5: Mixing

After some discussion we opted to mix it ourselves for a few reasons.

It's our first EP, it's not going to be heard by a huge amount of people and while it's important that it sounds good, it's expensive to get done properly and we might be able to manage 'good enough' on our own with a lot of work.Mixing as an online service seems to be the main way to go for small/starting out bands and that seems too creatively detached. There are many stories floating around of people who've sent things off for mixing and the mixer has a much different idea of how it sounds.
Maybe it's not the best choice, but it seemed an acceptable risk that we would at least attempt to have it 'look' how we want and it be a bit wonky than potentially end up with something we're not happy with in a different way. The benefit would have been being able to just ship the stems off and make it someone else's problem instead of weeks of being unsure while swimming in the…