views:

1775

answers:

2

I'm trying to combine a list of functions like so.

I have this:

Func<int, bool>[] criteria = new Func<int, bool>[3];
criteria[0] = i => i % 2 == 0;
criteria[1] = i => i % 3 == 0;
criteria[2] = i => i % 5 == 0;

And I want this:

Func<int, bool>[] predicates = new Func<int, bool>[3];
predicates[0] = i => i % 2 == 0;
predicates[1] = i => i % 2 == 0 && i % 3 == 0;
predicates[2] = i => i % 2 == 0 && i % 3 == 0 && i % 5 == 0;

So far I've got the following code:

Expression<Func<int, bool>>[] results = new Expression<Func<int, bool>>[criteria.Length];

for (int i = 0; i < criteria.Length; i++)
{
    results[i] = f => true;
    for (int j = 0; j <= i; j++)
    {
        Expression<Func<int, bool>> expr = b => criteria[j](b);
        var invokedExpr = Expression.Invoke(
            expr, 
            results[i].Parameters.Cast<Expression>());
        results[i] = Expression.Lambda<Func<int, bool>>(
            Expression.And(results[i].Body, invokedExpr), 
            results[i].Parameters);
    }
}
var predicates = results.Select(e => e.Compile()).ToArray();

Console.WriteLine(predicates[0](6)); // Returns true
Console.WriteLine(predicates[1](6)); // Returns false
Console.WriteLine(predicates[2](6)); // Throws an IndexOutOfRangeException

Does anyone know what I'm doing wrong?

+3  A: 

This was a guess, as I know little about this stuff, but this seems to fix it:

Func<int, bool>[] criteria = new Func<int, bool>[3]; 
            criteria[0] = i => i % 2 == 0; 
            criteria[1] = i => i % 3 == 0; 
            criteria[2] = i => i % 5 == 0;
            Expression<Func<int, bool>>[] results = new Expression<Func<int, bool>>[criteria.Length];
            for (int i = 0; i < criteria.Length; i++)
            {
                results[i] = f => true; 
                for (int j = 0; j <= i; j++)
                {
                    int ii = i;
                    int jj = j;
                    Expression<Func<int, bool>> expr = b => criteria[jj](b); 
                    var invokedExpr = Expression.Invoke(expr, results[ii].Parameters.Cast<Expression>()); 
                    results[ii] = Expression.Lambda<Func<int, bool>>(Expression.And(results[ii].Body, invokedExpr), results[ii].Parameters);
                }
            } 
            var predicates = results.Select(e => e.Compile()).ToArray();

The key is the introduction of 'ii' and 'jj' (maybe only one matters, I didn't try). I think you are capturing a mutable variable inside a lambda, and thus when you finally reference it, you're seeing the later-mutated value rather than the original value.

Brian
That fixed it. Only jj mattered. Thanks
Cameron MacFarland
+2  A: 

No need to pull in Expressions...

    Func<int, bool>[] criteria = new Func<int, bool>[3];
    criteria[0] = i => i % 2 == 0;
    criteria[1] = i => i % 3 == 0;
    criteria[2] = i => i % 5 == 0;

    Func<int, bool>[] predicates = new Func<int, bool>[3];

    predicates[0] = criteria[0];
    for (int i = 1; i < criteria.Length; i++)
    {
        //need j to be an unchanging int, one for each loop execution.
        int j = i;

        predicates[j] = x => predicates[j - 1](x) && criteria[j](x);
    }

    Console.WriteLine(predicates[0](6)); //True
    Console.WriteLine(predicates[1](6)); //True
    Console.WriteLine(predicates[2](6)); //False
David B