Sunday, May 19, 2013

Capital Eye

What will the following line print out?

Console.WriteLine("i".ToUpper());

The answer is obvious: "It depends"

String.ToUpper as well as string.ToLower will use current thread culture, and in most cases it will print "I", but there is two cultures, where capital "i" is not "I", but "İ" - Turkish (tr) and Azeri Latin (az-Latn).

So never ever use ToUpper() and ToLower() in internal comparisons or serialization (I have found this while creating SQL queries, something like "... where id in [1]".ToUpper()), use ToLowerInvariant() and ToUpperInvariant() instead.

Compiled lambda issue



Never ever return the result of Expression<T>.Compile() method. If your compiled expression throws an exception, you will not get any info about the compiled lambda. For example, I have the NullReferenceException with following stack trace:


System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , BugDto )
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.GroupedEnumerable`3.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
...

There is nothing that could help me to find the cause of exception, I don't know where the expression was compiled or even created.

The best you can do is to return Expression<T> itself and use IQueryable methods instead of IEnumerable.

Monday, October 15, 2012

Queryable extensions

It is possible to extend a LINQ vocabulary with some custom methods that will be used later in query providers to build a custom query to outer source. Also, there is a little trick that allows using such extensions in Linq-to-objects expressions.

Here is a standard stub for an IQueryable method:


public static IQueryable<Tuple<int, TKey>> CountBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
{
    return source.Provider.CreateQuery<Tuple<int, TKey>>(
            Expression.Call(
              null,
              ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource), typeof(TKey)),
              new[] { source.Expression, Expression.Quote(keySelector) }
                                                       ));
}

To add this methods for LINQ-to-Object you have to add the method for IEnumerable as well with same parameters (just replace Expression<Func<...>> with Func<...>) to the same type where IQueryable method is defined:


public static IEnumerable<Tuple<int,TKey>> CountBy<TSource,TKey>(this IEnumerable<TSource> source, Func<TSource,TKey> keySelector)
{
    return source.GroupBy(keySelector).Select(x => Tuple.Create(x.Count(), x.Key));
}


Friday, September 21, 2012

Pearls of scala I - By-name parameters

In C# there is no way to define short-circuit evaluated 'and' function (for example).

The naive implementation like following is not good enough.

bool And(bool a, bool b){
  if (a)
    return b;
  else 
    return false;
}

Let define an identity function:


bool Id(bool value){
  Console.WriteLine(value);
  return value;
}


And let's compare output of the following statements:

And(Id(false),Id(false))    and    Id(false) && Id(false)

The first statement will produce:
False
False

The second one:
False

The second statement will not be fully evaluated. As far as first operand of && returns false, the statement evaluates to false regardless of second operand.

Instead the first one have to evaluate both parameters before evaluating the method body.

The only way to imitate the short-circuit evaluation is to pass ugly Action<bool> as a second parameter:


bool And(bool a, Action<bool> b){
  if (a)
   return b();
  else 
    return false;
}

var result = And(Id(false), ()=>Id(false));

In Scala, by-name parameters are part of language, so we could easily transform long-circuit 'and' function to  a short-circuit with minimum changes in function definition and without any changes in function usages:


Long-circuit:

def and(a:Boolean, b:Boolean) = if (a) b else false

Short-circuit:

def and(a:Boolean, b: => Boolean) = if (a) b else false

'=> Boolean' states for 'by-name' parameter, it would be evaluated only in function body as many times as requested (even zero, like in 'and(false,someLongFunction())')




Wednesday, May 30, 2012

MakeGenericType

When does the following line of code could fail with "The number of generic arguments provided doesn't equal the arity of the generic type definition"?


var t = typeof (SomeGenericClass<,,>).MakeGenericType (typeof (int), typeof (int), typeof (int));

Where SomeGenericClass is defined as:

public class SomeGenericClass<TT1T2>
{
}

Answer (thx for Elena):

If SomeGenericClass is defined inside another generic type, its generic types arguments will consist of root type generic arguments and its own generic arguments. MakeGenricType will cut of all generic type arguments, so you have to provide types for base type definition too:


public class SomeRootClass<T3>
{
  public class SomeGenericClass<TT1T2>
  {
  }
...

var t = typeof (SomeGenericClass<,,>).MakeGenericType (typeof (T3), typeof (int), typeof (int), typeof (int));
...
}

Thursday, May 24, 2012

Reverse

Three tiny puzzles:
  1. Given the following interface:

    interface
     INode
    {
             INode Next { getset; }
             object Data { get; }
    }

    and a reference to root 
    INode root reverse a one-linked list. Do not use any additional collections, as arrays, lists or stacks.
    
    
  2. The same for this interface:

    interface INode
    {
             INode Next { get}
             object Data { getset; }
    }

  3. And the same for this:

    interface
     INode
    {
             INode Next { get}
             object Data { get; }
    }
First and second tasks are almost the same, the 3rd solution will be good for 1st and 2nd as well.

Thursday, May 17, 2012

Mix of Queryable and Enumerable

Some useful observation from Expressions world.

Let's imagine we have the following class:


public class Foo
{
 public int[] Items { getset; }
}



And we use it in some IQueryable request in a following manner:


var queryable = new[] { new Foo {Items = new[] { 1, 2, 3 }} }.AsQueryable();

var q = queryable.Select(x => x.Items.Select(y => y % 2 == 0));


Note, that Items implements IEnumerable only, so this query is represented as:
var q = Queryable.Select(queryable, x => Enumerable.Select<intbool>(x.Items, y => y % 2 == 0));

Enumerable.Select takes Func<,> as its second parameters and we're expecting that this functor will be transformed into some method. But in this case, functor inside a Select method will be translated in Expressions as well:



ParameterExpression CS1 = Expression.Parameter(typeof(Foo), "x");
ParameterExpression CS3 = Expression.Parameter(typeof (int), "y");

MemberExpression memberExpression = Expression.Property(CS1, items);
IQueryable<IEnumerable<bool>> q1 = queryable.Select(
  Expression.Lambda<Func<FooIEnumerable<bool>>>(
    Expression.Call(null, select, new Expression[]
      {
        memberExpression,
        Expression.Lambda<Func<intbool>>(
          Expression.Equal(
            Expression.Modulo(CS3,
                              Expression.Constant(2, typeof (int))),
            Expression.Constant(0, typeof (int))), new[] {CS3})
      }), new[] {CS1}));
The same could be used for creating your own Expressions - if you use methods that accept functors, do not compile them in place, just insert as Expression.Lambda.

Note, that if Items implement IQueryable rather than IEnumerable, the nested Lamba expression will be quoted:
 
public class Bar
{
 public IQueryable<int> Items { getset; }
}
var queryable = new[] { new Bar {Items = new[] { 1, 2, 3 }.AsQueryable()} }.AsQueryable();

var q = queryable.Select(x => x.Items.Select(y => y % 2 == 0));

ParameterExpression CS1 = Expression.Parameter(typeof(Foo), "x");
ParameterExpression CS3 = Expression.Parameter(typeof (int), "y");

MemberExpression memberExpression = Expression.Property(CS1, items);
IQueryable<IEnumerable<bool>> q1 = queryable.Select(
  Expression.Lambda<Func<FooIEnumerable<bool>>>(
    Expression.Call(null, select, new Expression[]
      {
        memberExpression, Expression.Quote(
        Expression.Lambda<Func<intbool>>(
          Expression.Equal(
            Expression.Modulo(CS3,
                              Expression.Constant(2, typeof (int))),
            Expression.Constant(0, typeof (int))), new[] {CS3})
        )
      }), new[] {CS1}));
And this is the only purpose for Expression.Quote method.