Cook Computing

Tip: Convert Value or Reference to Sequence of Length One

May 28, 2009 Written by Charles Cook

In the previous post I originally created the sequences based on a single value by creating an array. For example:


static Expr Nil()
{
  return s => new string[] { s };
}

However this deviates a bit from the spirit of Linq, adds an unneccessary implementation detail, the fact that an array implements IEnumerable<T>, and hard-codes the string type into the expression. The neater way to do it is to use Enumerable.Repeat():


static Expr Nil()
{
  return s => Enumerable.Repeat(s, 1);
}