Coolest C# LINQ/Lambdas trick you've ever pulled?

Saw a post about hidden features in C# but not a lot of people have written linq/lambdas example so... I wonder...

What's the coolest (as in the most elegant) use of the C# LINQ and/or Lambdas/anonymous delegates you have ever saw/written?

Bonus if it has went into production too!


The LINQ Raytracer certainly tops my list =)

I'm not quite sure if qualifies as elegant but it is most certainly the coolest linq-expression I've ever seen!

Oh, and just to be extremely clear; I did not write it (Luke Hoban did)


Some basic functionals:

public static class Functionals
{
    // One-argument Y-Combinator.
    public static Func<T, TResult> Y<T, TResult>(Func<Func<T, TResult>, Func<T, TResult>> F)
    {
        return t => F(Y(F))(t);
    }

    // Two-argument Y-Combinator.
    public static Func<T1, T2, TResult> Y<T1, T2, TResult>(Func<Func<T1, T2, TResult>, Func<T1, T2, TResult>> F)
    {
        return (t1, t2) => F(Y(F))(t1, t2);
    }

    // Three-arugument Y-Combinator.
    public static Func<T1, T2, T3, TResult> Y<T1, T2, T3, TResult>(Func<Func<T1, T2, T3, TResult>, Func<T1, T2, T3, TResult>> F)
    {
        return (t1, t2, t3) => F(Y(F))(t1, t2, t3);
    }

    // Four-arugument Y-Combinator.
    public static Func<T1, T2, T3, T4, TResult> Y<T1, T2, T3, T4, TResult>(Func<Func<T1, T2, T3, T4, TResult>, Func<T1, T2, T3, T4, TResult>> F)
    {
        return (t1, t2, t3, t4) => F(Y(F))(t1, t2, t3, t4);
    }

    // Curry first argument
    public static Func<T1, Func<T2, TResult>> Curry<T1, T2, TResult>(Func<T1, T2, TResult> F)
    {
        return t1 => t2 => F(t1, t2);
    }

    // Curry second argument.
    public static Func<T2, Func<T1, TResult>> Curry2nd<T1, T2, TResult>(Func<T1, T2, TResult> F)
    {
        return t2 => t1 => F(t1, t2);
    }

    // Uncurry first argument.
    public static Func<T1, T2, TResult> Uncurry<T1, T2, TResult>(Func<T1, Func<T2, TResult>> F)
    {
        return (t1, t2) => F(t1)(t2);
    }

    // Uncurry second argument.
    public static Func<T1, T2, TResult> Uncurry2nd<T1, T2, TResult>(Func<T2, Func<T1, TResult>> F)
    {
        return (t1, t2) => F(t2)(t1);
    }
}

Don't do much good if you don't know how to use them. In order to know that, you need to know what they're for:

  • What is currying?
  • What is a y-combinator?

  • By far the most impressive Linq implementation i've ever come across is the Brahma framework.

    It can be used to offload parallel calculations to the GPU using 'Linq to GPU'. You write a 'query' in linq, and then Brahma translates it into HLSL (High Level Shader Language) so DirectX can process it on the GPU.

    This site will only let me paste one link so try this webcast from dotnetrocks:

    http://www.dotnetrocks.com/default.aspx?showNum=466

    Else google for Brahma Project, you'll get the right pages.

    Extremely cool stuff.

    GJ

    链接地址: http://www.djcxy.com/p/51332.html

    上一篇: LINQ to SQL是死还是活?

    下一篇: 你曾经拉过最酷的C#LINQ / Lambdas技巧吗?