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

后端 未结 14 1476
死守一世寂寞
死守一世寂寞 2020-12-22 17:00

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

14条回答
  •  渐次进展
    2020-12-22 18:02

    Some basic functionals:

    public static class Functionals
    {
        // One-argument Y-Combinator.
        public static Func Y(Func, Func> F)
        {
            return t => F(Y(F))(t);
        }
    
        // Two-argument Y-Combinator.
        public static Func Y(Func, Func> F)
        {
            return (t1, t2) => F(Y(F))(t1, t2);
        }
    
        // Three-arugument Y-Combinator.
        public static Func Y(Func, Func> F)
        {
            return (t1, t2, t3) => F(Y(F))(t1, t2, t3);
        }
    
        // Four-arugument Y-Combinator.
        public static Func Y(Func, Func> F)
        {
            return (t1, t2, t3, t4) => F(Y(F))(t1, t2, t3, t4);
        }
    
        // Curry first argument
        public static Func> Curry(Func F)
        {
            return t1 => t2 => F(t1, t2);
        }
    
        // Curry second argument.
        public static Func> Curry2nd(Func F)
        {
            return t2 => t1 => F(t1, t2);
        }
    
        // Uncurry first argument.
        public static Func Uncurry(Func> F)
        {
            return (t1, t2) => F(t1)(t2);
        }
    
        // Uncurry second argument.
        public static Func Uncurry2nd(Func> 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?

提交回复
热议问题