Additional lambda expressions support

1- An easier syntax to combine functions:
Given:



int[] numbers = {0, 1, 2, 3, 4};
Func<int, bool> isOdd = i => i % 2 != 0;
Func<int, bool> isZero = i => i == 0;

 


Instead of having:


Func<int, bool> isZeroOrOdd = i => isZero(i) || isOdd(i);

 


We could have:


Func<int, bool> isZeroOrOdd = isZero || isOdd;

 


2- Same thing but with expressions:
Given:


Expression<Func<int, bool>> isOdd = i => i % 2 != 0;
Expression<Func<int, bool>> isZero = i => i == 0;

 


It would be great to have:


Expression<Func<int, bool>> isZeroOrOdd = isZero || isOdd

 


which would then result into a new binary expression (OR) from the two isZero and isOdd expressions.



Answer this question

Additional lambda expressions support

  • tlathrop

    I think we should do this too. Maybe I'll just slip it in while everyone else is asleep



  • Raja Annamalai

    It would be possible, given inerference, to come up with type ambiguity in the final function.  Say that the input functions take int or string -- you'd have to specify which versions of the functions to use.

    Personally, I like the syntax.  It reminds me of operator math from my quantum physics classes in college.


  • Jessica.Weiner

    Yeah I figured that out afterwards, still it would be greatly useful to have that kind of syntax for expressions:


    Func<int, int, bool> foo = (i,j) => i == j;
    Func<int, int, bool> bar = (i, j) => i % j == 0;
    Func<int, int, bool> baz = (i,j) => foo(i,j) || bar(j,i);

     


    It would me awesome it the equivalent for expressions could exist:

    Expression<Func<int, int, bool>> fooExpression = (i,j) => i == j;
    Expression<Func<int, int, bool>> barExpression = (i, j) => i % j == 0;
    Expression<Func<int, int, bool>> bazExpression = (i,j) => foo(i,j) || bar(j,i);

     


    But there are currently no other ways to create the bazExpression than to create the expression tree manually by creating a new BinaryExpression with the foo, bar as parameters, ...

  • jaxrpc

    Think you could slip in Expression serialization while you're there ;)

  • www.GaaLive.com

    That might end up being too terse. Good idea though.

  • El Pea

    This breaks quickly in the multivariate case:

    Given foo(int, int) and bar(int, int),

       Baz = foo [op] bar

    could generate 

       foo(a, b) [op] bar(a, b)

    when it is entirely possible to want

       foo(a, b) [op] bar(b, a)



  • Additional lambda expressions support