Cook Computing

«  Tip: Convert Value or Reference to Sequence of Length One  »

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);
}
Posted by Charles Cook at 08:55 AM. Permalink.
blog comments powered by Disqus.