Blog Archive

Friday, November 16

New blog

For no good reason, I installed Windows Live writer and started a new blog. You can check it out at http://jacobcarpenter.wordpress.com/.

Thursday, July 26

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloOrcas
{
    class Program
    {
        static void Main(string[] args)
        {
            "Hello, Orcas!".Print();
        }
    }

    public static class StringExtensions
    {
        public static void Print(this string message)
        {
            Console.WriteLine(message);
        }
    }
}

Wednesday, April 4

    7 public static class CollectionHelper
    8 {
    9     public static IEnumerable<T> Enumerate<T>(params T[] args)
   10     {
   11         if (args == null)
   12             throw new ArgumentNullException("args");
   13 
   14         foreach (T arg in args)
   15             yield return arg;
   16     }
   17 
   18     public static T First<T>(IEnumerable<T> source)
   19     {
   20         return First(source, ReturnTrue);
   21     }
   22 
   23     public static T First<T>(IEnumerable<T> source, Func<T, bool> predicate)
   24     {
   25         T result;
   26         if (!TryFindFirst(source, out result, predicate))
   27             throw new InvalidOperationException("Source sequence empty or no items satisfy predicate.");
   28 
   29         return result;
   30     }
   31 
   32     public static T FirstOrDefault<T>(IEnumerable<T> source)
   33     {
   34         return FirstOrDefault(source, ReturnTrue);
   35     }
   36 
   37     public static T FirstOrDefault<T>(IEnumerable<T> source, Func<T, bool> predicate)
   38     {
   39         T result;
   40         TryFindFirst(source, out result, predicate);
   41         return result;
   42     }
   43 
   44     public static bool TryFindFirst<T>(IEnumerable<T> source, out T result)
   45     {
   46         return TryFindFirst(source, out result, ReturnTrue);
   47     }
   48 
   49     public static bool TryFindFirst<T>(IEnumerable<T> source, out T result, Func<T, bool> predicate)
   50     {
   51         if (source == null)
   52             throw new ArgumentNullException("source");
   53         if (predicate == null)
   54             throw new ArgumentNullException("predicate");
   55 
   56         foreach (T item in source)
   57         {
   58             if (predicate(item))
   59             {
   60                 result = item;
   61                 return true;
   62             }
   63         }
   64 
   65         result = default(T);
   66         return false;
   67     }
   68 
   69     public static IEnumerable<TOut> Map<T, TOut>(IEnumerable<T> source, Func<T, TOut> converter)
   70     {
   71         if (source == null)
   72             throw new ArgumentNullException("source");
   73         if (converter == null)
   74             throw new ArgumentNullException("fn");
   75 
   76         foreach (T item in source)
   77             yield return converter(item);
   78     }
   79 
   80     public static IEnumerable<TOut> Zip<TLeft, TRight, TOut>(IEnumerable<TLeft> left, IEnumerable<TRight> right, Func<TLeft, TRight, TOut> fn)
   81     {
   82         if (left == null)
   83             throw new ArgumentNullException("left");
   84         if (right == null)
   85             throw new ArgumentNullException("right");
   86         if (fn == null)
   87             throw new ArgumentNullException("fn");
   88 
   89         using (IEnumerator<TLeft> leftEnumerator = left.GetEnumerator())
   90         using (IEnumerator<TRight> rightEnumerator = right.GetEnumerator())
   91         {
   92             while (true)
   93             {
   94                 bool leftMoved = leftEnumerator.MoveNext();
   95 
   96                 if (!(leftMoved == rightEnumerator.MoveNext()))
   97                     throw new InvalidOperationException("Sequences are of incompatible lengths.");
   98 
   99                 if (!leftMoved)
  100                     break;
  101 
  102                 yield return fn(leftEnumerator.Current, rightEnumerator.Current);
  103             }
  104         }
  105     }
  106 
  107     private static bool ReturnTrue<T>(T ignored)
  108     {
  109         return true;
  110     }
  111 }

Tuesday, November 14

Single Raised EventHandler

Earlier today, a friend described a scenario at his work where he needed to hook up an EventHandler that was only raised once.

A naïve solution could be to merely check whether the critical code had been executed within the handler; then set the state appropriately upon the first execution. Something like:

public class TestClass
{
   public event EventHandler SimpleEvent;

   public void RaiseAll()
   {
      if (SimpleEvent != null)
         SimpleEvent(null, EventArgs.Empty);
   }
}

// elsewhere:

TestClass test = new TestClass();

bool raised = false;
test.SimpleEvent += delegate
{
   if (!raised)
   {
      // critical code:
      raised = true;
      Console.WriteLine("Hello world!");
   }
};

test.RaiseAll();
test.RaiseAll();
test.RaiseAll();

// output:
// Hello world!

Okay, that's fine for an event on a simple, transient instance of an object. But what if, in the lifecycle of our application, we could potentially throw away hundered of EventHandlers? And what if, further complicating the problem, the event is static? It sure would be nice if we could actually remove the EventHandler, once it has been raised.

Well, we can. There are two ways to achieve this: one is simple but requires duplication for reuse; the other is complicated but easily reusable.

Let's look at the simple one first:

TestClass test = new TestClass();

EventHandler handler = null; // avoid: error CS0165: Use of unassigned local variable 'handler'
test.SimpleEvent += handler = delegate
{
   test.SimpleEvent -= handler; // remove self before executing the critical code
   Console.WriteLine("Hello world!");
};

test.RaiseAll();
test.RaiseAll();
test.RaiseAll();

// output:
// Hello world!

There are a couple of things to note here:

  1. Since we know the type of TestClass's SimpleEvent, we can declare a strongly typed EventHandler and assign an anonymous method to it. Not knowing this type at compile-time is the source of much of the complication of the reusable solution below.
  2. Also, we temporarily assign null to handler before referring to handler within the body of the anonymous method to avoid the noted compiler error. Then, we assign the anonymous method to handler before attaching handler to the event.

But, Jacob, this works fine. Why would we care about "improving" it?

Well, for starters, it's not very extensible. The critical code is embedded into the anonymous method. So anytime we want to bring different functionality to this event, we'll need to repeat this pattern. Also, not only is the critical code not pluggable, but we've constrained ourselves to only EventHandler events. There are other types of strongly typed event handling delegates with far more interesting EventArgs (and how does the name "EventArgs" not violate the Framework Design Guidelines, anyway?). And lastly, … well… because we can:

[At this point in the post, the author suddenly switches voices: the hand-holdy, instructive teacher is replaced with the programmer who has spent too much time with the material at hand and pastes in swaths of code assuming his audience will understand. Apologies for the lack of exposition to follow.]

public static class EventUtility
{
   public static void AttachRaisedOnce<TTarget>(TTarget target, string eventName, EventHandler raisedOnce)
      where TTarget : class
   {
      AttachRaisedOnce<TTarget, EventArgs>(target, eventName, CastDelegate<EventHandler<EventArgs>>(raisedOnce));
   }

   public static void AttachRaisedOnce<TTarget, TEventArgs>(TTarget target, string eventName, EventHandler<TEventArgs> raisedOnce)
      where TTarget : class
      where TEventArgs : EventArgs
   {
      EventInfo eventTarget = typeof(TTarget).GetEvent(eventName);
      if (eventTarget == null)
         throw new ArgumentException(String.Format("Couldn't find event with name '{0}'", eventName), "eventName");

      Delegate self = null; // avoid unassigned local
      EventHandler<TEventArgs> localMethod = delegate(object sender, TEventArgs e)
      {
         eventTarget.RemoveEventHandler(target, self);
         raisedOnce(sender, e);
      };

      self = Delegate.CreateDelegate(eventTarget.EventHandlerType, localMethod.Target, localMethod.Method);
      eventTarget.AddEventHandler(target, self);
   }

   // see earlier post
   // also: belongs elsewhere; maybe a static DelegateUtility class
   private static T CastDelegate<T>(Delegate source)
      where T : class // CS0702: Constraint cannot be special class 'System.Delegate'
   {
      if (source == null)
         return null;

      Delegate[] delegates = source.GetInvocationList();
      if (delegates.Length == 1)
         return Delegate.CreateDelegate(typeof(T), delegates[0].Target, delegates[0].Method) as T;

      for (int i = 0; i < delegates.Length; i++)
         delegates[i] = Delegate.CreateDelegate(typeof(T), delegates[i].Target, delegates[i].Method);

      return Delegate.Combine(delegates) as T;
   }
}

Calling code looks like:

TestClass test = new TestClass();

EventUtility.AttachRaisedOnce(test, "SimpleEvent", delegate { Console.WriteLine("Hello world!"); });

test.RaiseAll();
test.RaiseAll();
test.RaiseAll();

// output:
// Hello world!

For strongly typed EventHandlers, like the System.Web.UI.ImageClickEventHandler delegate, the calling code looks a little weird:

EventUtility.AttachRaisedOnce<ImageButton, ImageClickEventArgs>(button, "Click", delegate { Response.Write("Hello web!"); });

Note that the second type argument is the type of the EventArgs, not the type of the EventHandler delegate itself. This is due to the inablity to apply a Delegate constraint on a type argument, coupled with the need to assign an anonymous method to a local variable with the right type. We rely on the fact that we can convert from an EventHandler<T> to the strongly typed EventHandler delegate. This means that it is also possible to compile code with the wrong event/delegate combinations; but don't worry: the runtime will "inform you" of any conversion failures.

Enjoy.

Friday, November 3

Enum TryParse

I was somewhat surprised to discover that there's no TryParse method for the Enum class in the .NET Framework 2.0. (It's also odd that the docs/compiler keep referring to Enum as a class—and yet the apprpriate where constraint to apply is struct.) So here's a pair of generic methods to support TryParse for Enums:

public static class EnumUtility
{
   public static bool TryParse<T>(string value, out T result)
      where T : struct // error CS0702: Constraint cannot be special class 'System.Enum'
   {
      return TryParse<T>(value, out result, false);
   }

   public static bool TryParse<T>(string value, out T result, bool ignoreCase)
      where T : struct // error CS0702: Constraint cannot be special class 'System.Enum'
   {
      result = default(T);
      try
      {
         result = (T)Enum.Parse(typeof(T), value, ignoreCase);
         return true;
      }
      catch { }

      return false;
   }
}

Tuesday, October 17

Enumerate Using

If you ever need to enumerate a collection of IDisposable items, and you find the following code snippet ugly:

foreach (SomeDisposableClass item in collectionOfDisposableItems)
{
   using (item)
   {
      // do stuff
   }
}

You may find the following method useful:

public static IEnumerable<T> EnumerateUsing<T>(IEnumerable<T> disposableItems)
   where T : IDisposable
{
   foreach (T item in disposableItems)
      using (item)
         yield return item;
}

Calling code looks like:

foreach (SomeDisposableClass item in EnumerateUsing(collectionOfDisposableItems))
{
   // do stuff
}

And yes, Dispose() does get called appropriately after you're finished "doing stuff".

Friday, October 6

Merging in Subversion

Once you've done it, merging from a branch to the trunk (or vice versa) in Subversion is a trivial task. But that first time you attempt it, it can seem a little daunting. Here's a quick how-to:

  1. Let's say you've got a development branch that's all committed and ready to be merged into the trunk. First, switch your working copy to where the result of the merge will ultimately be committed; in this case, the trunk.
  2. merge from the current location to the location that resembles your desired final result; for us, this is merging from the trunk to the development branch.
  3. Resolve any conflicts and commit your current working copy.

Friday, September 15

Visual Studio Macro

Never thought I'd be posting VB to my blog, but here's a little Visual Studio macro to switch between a Class file and it's associated TestFixture. The macro assumes a ClassNameFixture naming convention. Also sorry if my VB sucks, it literally took me 3 minutes plus asking a coworker to figure out how test for null.

Enjoy:

Imports System
Imports System.IO
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module TestFixtures

    Public Sub SwitchBetweenClassAndFixture()
        Dim active As Document, filename As String, extension As String
        Const fixtureSuffix = "Fixture"

        active = DTE.ActiveDocument
        If Not active Is Nothing Then
            filename = Path.GetFileNameWithoutExtension(active.Name)
            extension = Path.GetExtension(active.Name)
            If filename.EndsWith(fixtureSuffix) Then
                DTE.ExecuteCommand("Edit.OpenFile", filename.Substring(0, filename.Length - fixtureSuffix.Length) + extension)
            Else
                DTE.ExecuteCommand("Edit.OpenFile", filename + fixtureSuffix + extension)
            End If
        End If
    End Sub

End Module

Wednesday, August 2

Generics and Delegates

I see a surprising number of search hits on this blog for things involving generics, delegates, and collections. So I guess it would be appropriate to write a series of posts on the subject.

This is all pretty rudimentary stuff, so if you're familiar with generics, delegates, and anonymous methods, you may want to just skip this one.

Generics

public static class Assert
{
    public void Throws<T>(/* ... */) where T: Exception {/* ... */}
}

Here we have a method, named as if it were a part of some test framework (say, NUnit). This method takes one type parameter (T), so we can call this method a "generic method".

This method also contains what is called a "type constraint". That bit beginning with where says that any type that stands in for T must derive from (or be) System.Exception. There are other types of… well… type constraints that I don't think I'll cover in this post.

Okay, so what would we need to know to see if a block of code threw a specific exception? Obviously the type of exception we would expect—that's T. But we would also need the block of code, wouldn't we?

Delegates / Anonymous methods

Delegates have been in C# since forever. Event handling in Forms (and Web Forms) uses delegates extensively. So what's the big fuss?

Well, C# 2.0 introduces the very useful concept of anonymous methods. Arbitrary blocks of code that can be passed around and executed at whim.

Wheras before one had to define a method corresponding to a specific delegate's signature, and then construct the delegate (using syntax that still feels a little odd) before being able to pass it around, you can now say:

public delegate void VoidNoArg();

// ...

VoidNoArg myCode = delegate { Console.WriteLine("Hello world!"); };

Neat. But how useful can a void, no argument delegate really be? Very, actually, through the magic of closures. That is, you can actually say:

string greeting = "Hello world!";

VoidNoArg myCode = delegate { Console.WriteLine("Hello world!"); };
VoidNoArg myCode = delegate { Console.WriteLine(greeting); };

myCode();

// output: Hello world!

Note that the anonymous method referrs to a variable outside of its own scope.

Putting it together

public static class Assert
{
    public void Throws<T>(VoidNoArg code) where T: Exception {/* ... */}
}

// ...

int i = Int32.MaxValue;
Assert.Throws<OverflowException>(delegate { checked(i + 1); });

That's just the tip of the iceberg.

Delegates can be generic. Classes can be generic. Generic classes can have methods that take generic delegates. The System.Collections.Generic collections do all of these things. We'll look at this more in a future post…

Tuesday, July 18

Ruby Closures

This guide covering closures in Ruby by Paul Cantrell is very insightful. It's written as a Ruby script to be both executed and read. Thanks to Ruby Inside for linking to it.

Tuesday, July 11

JSON Serialization Using C# 3.0 Extension Methods

This post on JSON Serialization Using Extension Methods is a nice demonstration of an elegant solution made possible with extension methods. Nice job, Keith.

Friday, July 7

Collection<T> and explicit interface implementations

Say you're looking at Collection<T> and ReadOnlyCollection<T> in the System.Collections.ObjectModel namespace. You think, “I could use those! In fact, there's a collection I've been meaning to write for awhile:”

class StringCollection : Collection<string>
{
    public StringCollection(params string[] strings) : base(strings) { }

    public string Join(string separator)
    {
        // clearly this is not an efficient solution!
        string[] strings = new string[Items.Count];
        Items.CopyTo(strings, 0);

        return String.Join(separator, strings);
    }
}

class Program
{
    static void Main(string[] args)
    {
        StringCollection strings = new StringCollection("one", "two", "three");
        Console.WriteLine(strings.Join(", "));
    }
}

// Output:
// one, two, three

Perfect! That's exactly what you expected.

Next, you decide to modify your program. You want to add another string:

static void Main(string[] args)
{
    StringCollection strings = new StringCollection("one", "two", "three");
    strings.Add("four");
    Console.WriteLine(strings.Join(", "));
}

Builds fine and… NotSupportedException Collection is read-only!

But wait, we inherited from Collection<T>, not ReadOnlyCollection<T>. What gives?

Well, when we called the base constructor for Collection<T>, we gave it an IList<string>. Okay, so really we gave it an Array of strings, but it was implicitly converted to an IList<string>. That IList was stored as a field within the Collection wrapper (accessible via the protected Items property).

Calling Add calls the explicitly implemented interface member IList.Add on the string[] which (from the docs:) Throws a not supported exception in all cases.

So, how can we modify the StringCollection so that it continues to call the appropriate base constructor, but coerces the string[] to something a bit more useful?

One possibility is to check the IsReadOnly property of the IList before passing it along, like so:

class StringCollection : Collection<string>
{
    public StringCollection() : base() { }
    public StringCollection(params string[] strings) : this((IList<string>) strings) { }
    public StringCollection(IList<string> strings) : base(strings.IsReadOnly ? new List<string>(strings) : strings) { }

    // ...
}

I'm sure there are better ways, though. Any ideas?

Monday, July 3

NDoc

Right about the time I discovered NDoc, we started running pre-release bits of the 2.0 .NET Framework. It was pretty frustrating to discover such a useful tool that didn't work at all with the new features. Even more frustrating has been the lack of updates as I've checked back in on the project.

Well, it turns out Jonas Lagerblad is my 2.0 documentation generation hero. It looks like the updated NDoc still isn't compatible with Visul Studio 2005 solution files, but pointing at an assembly+generated xml works great. Thanks, Jonas!

Friday, June 30

Cast Delegate

Following my previous post, Ed went ahead and made a (C# 2.0-usable) Multicast-capable version:

public static T CastDelegate<T>(Delegate source)
    where T : class
{
    if (source == null)
        return null;

    Delegate[] delegates = source.GetInvocationList();
    if (delegates.Length == 1)
        return Delegate.CreateDelegate(typeof(T), delegates[0].Target, delegates[0].Method) as T;

    for (int i = 0; i < delegates.Length; i++)
        delegates[i] = Delegate.CreateDelegate(typeof(T), delegates[i].Target, delegates[i].Method);
 
    return Delegate.Combine(delegates) as T;
}

C# 3.0 and Delegate Conversion

System.Query defines a number of delegate types to help with query expressions. They follow the pattern of:

delegate T Func<T>();
delegate T Func<A0, T>(A0 arg0);
delegate T Func<A0, A1, T>(A0 arg0, A1 arg1);
// etc.

This is all well and good.

Certain methods in System.Query.Sequence take a parameter of Func<T, bool>. An example is Where<T>(this IEnumerable<T> source, Func<T, bool> predicate). Note that the underlying signatures of Func<T, bool> and Predicate<T> match.

Well it turns out that Delegates are kind of funky. For one thing, the Delegate class cannot be used as a generic type constraint. Also, conversion between compatible types of delegates isn't allowed (that is, you can't cast them).

That means that if one has a reference to an existing Predicate<T> one cannot use it as an argument to Where. I'm sure it won't come up much, because it's not often that one has a reference to a delegate without being able to reach the method that the delegate referrs to, but it could happen.

The best way I came up with to convert between two types of compatible delegates:

// CS0702: Constraint cannot be special class 'System.Delegate'
public static T ConvertTo<T>(this Delegate source)
    where T : class //, Delegate
{
    if (source.GetInvocationList().Length > 1)
        throw new ArgumentException("Cannot safely convert MulticastDelegate");

    return Delegate.CreateDelegate(typeof(T), source.Target, source.Method) as T;
}

This isn't great. Something like the following compiles just fine:

Predicate<int> isEven = n => n % 2 == 0;
MailAddress email = isEven.ConvertTo<MailAddress>();

Since Delegate is invalid as a type constraint, there's not really any way around it. I don't think that will ever cause any difficulty, though.

More subtly evil, however, is that conversion between incompatible delegates will appear to work fine at compile time, but result in a runtime error. Oh well. At least now I can do this:

int[] numbers = { 1, 2, 3, 4, 5 };
Predicate<int> isEven = n => n % 2 == 0;
foreach (var n in numbers.Where(isEven.ConvertTo<Func<int, bool>>()))
    Console.WriteLine(n);

// Output:
// 2
// 4

… not that I ever would…

Update: the delegate conversion method has been extended/updated.

Thursday, June 29

Best Visual Studio Help Option

Best Visual Studio Help option I've unchecked, today: Tools > Options > General: Reuse topic window. Now hitting F1 in Visual studio is a lot more like my web browsing experience (external links open a new tab within Firefox). Ctrl-F4 or middle-click the tab to close it.

Update: to be clear, I'm talking about Help/Documentation Viewer/what used to be called MSDN; not actually Visual Studio itself.

Tuesday, June 27

Testing internal code

A few upfront assertions:

  1. Unit testing is good. For C# code I use NUnit (along with TestDriven.NET).
  2. Languages (including C#) have been designed with the internal keyword for a reason. There are legitimate uses for something that's accessible only within one's own assembly.
  3. These internal classes/methods are reasonable candidates for unit testing.

So, how do you test internal bits without compiling test code into your deliverable assembly?

It turns out that it's trivial (in the .NET 2.0 Framework): Friend Assemblies (see also).

Say you have two projects: MyCoolAssembly and MyCoolAssembly.UnitTests.

  1. In your MyCoolAssembly, open up the Properties/AssemblyInfo.cs.
  2. Add [assembly: InternalsVisibleTo("MyCoolAssembly.UnitTests")]

That's it! Now, MyCoolAssembly.UnitTests can access all of the internal classes/methods within MyCoolAssembly.

No conditional compilation symbols. No messy inner classes. Problem solved.

I found out about this useful attribute in CLR via C#, but Jay Bazuzi (whose blog I'd really like to read except that he apparently doesn't post anymore) had already blogged about this very usage back in September of 2004.

Thursday, June 22

Ruby's Enumerable mixin again

On the topic of Ruby's Enumerable mixin, it might be interesting to compare them to C# 2.0's Array/List<T> methods—static for Array, instance for List<T>—and 3.0's Standard Query Operators (System.Query namespace).

In the cases where C# does not support the method, I'd recommend following the link to the ruby docs. There are some rather interesting methods, and a lot can be learned from looking at what other languages support. In fact, some of these methods (each_slice, each_with_index) might be worth implementing as extension methods in C# (a trivial excercise left to the reader).

Ruby Enumerable C# 2.0 Array/List<T> methods* C# 3.0 System.Query
all?
all? { |obj| block }
bool TrueForAll(Predicate<T> match) bool All<T>(this IEnumerable<T> source, Func<T, bool> predicate)
any?
any? { |obj| block }
bool Exists(Predicate<T> match) bool Any<T>(this IEnumerable<T> source, Func<T, bool> predicate)
collect { |obj| block }
map { |obj| block}
List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) IEnumerable<S> Select<T, S>(this IEnumerable<T> source, Func<T, S> selector)
detect(ifnone = nil) { |obj| block }
find(ifnone = nil) { |obj| block }
T Find(Predicate<T> match) T First<T>(this IEnumerable<T> source, Func<T, bool> predicate)
T FirstOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate)
each_cons(n) {...}
each_slice(n) {...}
each_with_index { |obj, index| block }
to_a
entries
T[] ToArray<T>(this IEnumerable<T> source)
List<T> ToList<T>(this IEnumerable<T> source)
find_all { |obj| block }
select { |obj| block }
List<T> FindAll(Predicate<T> match) IEnumerable<T> Where(this IEnumerable<T> source, Func<T, bool> predicate)
grep(pattern) FindAll could certainly specify a predicate that applied a regular expression against each item of the collection. same for Where.
include?(obj) bool Contains(T item)
inject(initial) { |memo, obj| block }
inject { |memo, obj| block }
U Aggregate<T, U>(this IEnumerable<T> source, U seed, Func<U, T, U> func)
max / min
max / min { |a, b| block }
T Max/Min<T>(this IEnumerable<T> source), etc. Note that while there are a few overloads for this method, it looks like the delegates passed in merely provide value extraction. I don't see an overload where you can provide your own IComparer<T> (which is what the second Ruby method does)
partition { |obj| block }
reject { |obj| block } merely returns the collection of the inverse of find_all / select
sort
sort { |a, b | block }
sort_by { |obj| block }
Sort()
Sort(IComparer<T> comparison)
OrderedSequence<T, K> OrderBy/OrderByDescending/ThenBy/ThenByDescending(
  this IEnumerable<T> source, Func<T, K> keySelector)

OrderedSequence<T> OrderBy/OrderByDescending/ThenBy/ThenByDescending(
  this IEnumerable<T> source, Func<T, K> keySelector, IComparer<K> comparer)
to_set
zip(arg, ...)
zip(arg, ...) { |arr| block }

Enumerable mixin

I really like Ruby's Enumerable mixin. Methods like each_slice and each_with_index can inspire some really straightforward, elegant solutions.

Porting such a library to C# is trivial, but having to use the 2.0's verbose delegate syntax detracts from the coolness a bit. Using 3.0's lambda expressions on the other hand…

Okay, blogger's template is obviously not well suited to preformatted text. I should try and fix that sometime…