Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Why is C++ awesome?

C++ templates (similar to Java Generics, but pretty different at the same time) can also help significantly cut down duplicate code and may be an elegant solution for many use cases. Overall, C++ is definitely more verbose than many new programming languages like Kotlin and Python.

Where are C++ libraries located?

Usually, there is '/lib' folder on Windows or '/usr/lib' folder on Linux that contains all the libraries. Once the library is installed, the compiler and the linker know the path of the library to use, and the library is ready for use.


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


Progress Reporting for long running LINQ queries. In the blog post you can find an extension method WithProgressReporting() that lets you discover and report the progress of a linq query as it executes.


http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/

http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/