The .NET Framework started from the ground with a powerful advantage: language interoperability. In the last few years lots of languages that target the platform has appeared, and even other implementation of the CLI such as Mono.
On my first post, I would like to show you this multiplicity along with LINQ. I'll start with C#: the language have taken some improvements to allow easier writing of queries. The greates of all is the lambda expressions, little expressions whose type are inference and that are anonymous methods on asteroids:foreach (string item in list.Where (item => item.StartsWith("A"))
In that piece of code you can see another invention: extension methods. I'll take a little more about them another day. Finally, some syntactic sugar has been added to the language to look like SQL queries. However, I think the most interesting thing about LINQ is the availability of a library that allows to work on lots of data. Indeed, if I write:foreach (string item in
(from item in list where item.StartsWith("A") select item)) code
the bold text get compiled to exactly the same code as above (in this case it is more verbose, but SQL syntax helps on more difficult queries that imply joins or grouping).
Let's now go to F#: a functional language that is being developed in Microsoft Research. For those how doesn't know, a functional language works with data and functions that manage the data instead of classes that encapsulate methods.
In that language, they have implemented query expressions on a very funny way (this information has been mostly taken from Don Syme F# Weblog
). First of all, a new operator |>
has been implemented:let (|>) x f = f x
That means that if I write x |> f
, what I get executed if f (x)
. Then, they have select, order... functions expressed:let select f coll = System.Query.Sequence.Select(coll,new Func<_,_>(f))
let where f coll = System.Query.Sequence.Where(coll,new Func<_,_>(f))
So now, they use:let results = list |> where (fun item -> item.StartsWith ("A"))
Interesting, isn't it? As you can see, you can use LINQ from a lot of different languages, and you will get the same results.