-
Last time I wrote Shuffle and TakeRandom extension methods for IEnumerable<T> which use the Fisher-Yates algorithm to perform an unbiased shuffle of a sequence and take a random subset from it. In order to optimise the performance of the TakeRandom method I had to reverse the documented implementation...
-
It's a fairly common practice on web sites to display sets of randomly ordered items, whether it's a selection of your friends, items related to the one you're looking at, or even something as simple as a tag cloud. Sometimes the entire set of items is randomised, and sometimes you want to...
-
I don't know for sure, but my guess is because there are at least two implementations with different semantics: // immediate execution public static IEnumerable <T> ForEach<T>( this IEnumerable <T> source, Action <T> action) { foreach ( var item in source) { action(item);...
-
After my post about deferred execution semantics and re-entrancy I've had a couple of people ask why iterator code has deferred execution semantics rather than just executing immediately. To understand this we need to see how iterators are actually implemented. Consider the following very simple...
-
I've been a bit too busy with work and other things to finish the Linq translation posts, though I will return to them when I get some free time. For now I'll post some other things I had already drafted, and which are probably more interesting to most people anyway. Last year I noted that most...
-
This is the fourth entry in a series demonstrating how C# 3.0 query syntax is translated into the underlying method calls for Linq-to-Objects, and that you can write your own versions of the Linq extension methods. The previous entries were: Part 1: Introduction and Select Part 2: SelectMany Part 3:...
-
This is the third entry in a series demonstrating how C# 3.0 query syntax is translated into the underlying method calls for Linq-to-Objects, and that you can write your own versions of the Linq extension methods. The previous entries were: Part 1: Introduction and Select Part 2: SelectMany Now we know...
-
This is the second entry in a series demonstrating how C# 3.0 query syntax is translated into the underlying method calls for Linq-to-Objects, and that you can write your own versions of the Linq extension methods. In the first entry I outlined the types and collections I'd be using for this series...
-
The Linq query syntax introduced in C# 3.0 is an extremely powerful way to manipulate collections of objects. Depending on the query provider the expression may be translated in a number of ways, such as SQL statements for Linq-to-SQL or XQuery expressions for Linq-to-XML , but the simplest is when you're...
-
If you create a method to return a random set of items from an enumerable list, implemented using an iterator... public static IEnumerable<T> GetRandom<T>(this IEnumerable<T> items) { var random = new Random(); foreach (var item in items) { if (random.Next() < 0.5) { yield return...