Monday, December 21, 2009

System.Tuple: More .NET 4.0 Functional Goodness

In functional languages, tuples are pretty much a requirement.  Of the functional languages I’ve used, including Erlang and T-SQL, tuples are bread’n’butter.  Every release of .NET has included more and more support for functional programming paradigms, and we’re getting tuple support in the BCL in .NET 4.0, though first-class language support is still not there.  Here’s an example of using System.Tuple with the new Zip LINQ operator.

Code Snippet
  1. var xs = new List<int> { 1, 3, 5 };
  2. var ys = new List<string> { "hello", "world", "channel" };
  3. var zs = xs.Zip(ys, Tuple.Create);
  4.  
  5. foreach (var z in zs)
  6.     Console.WriteLine("{1}:{0}",z.Item1, z.Item2);