Episode 6 was all about LINQ, my favorite “feature” of C#/.NET. It offers a level of expressiveness that’s on par with dynamic languages without losing the benefits of compile time checks and performance.
So, if LINQ is so great…
Why is there no LINQ for Java?
Well, okay…Java 8 has lamdas and we’ll take a look at a few 3rd party libraries that emulate LINQ but there are a few fundamental limitations of the JVM that prevent.
But first, let’s take a look at the pieces of .NET and C# that make up LINQ.
CLR Features
Compiler Features
- Lambda / Comprehension Syntax (prettier delegates)
- Type inference – no string x => x.Trim()
- Extension Methods
- Yield Statement
The compiler features aren’t required, they’re the sugar. LINQ would survive without the syntax, type inference, and implementation via extension methods.
The CLR features are another story. These are the things that make LINQ…LINQ!
So, what does the CLR have that the JVM doesnt?
But is it really a killer feature? Obviously Java is immensely popular and there are loads of other languages that get a long just fine without it. I’ve been talking about type inference and higher order functions as if they’re something new. Far from it.
Lisp was designed in 1958!!!
That kind of takes the wind out of my .NET sails! However, C# and Lisp are very different beasts and were designed for very different problems. It’s great that I can harness some of that power in my day job.
In addition to languages that have native support this type of coding, there are a number of implementations in other languages on Wikipedia. C# is still my favorite. 🙂
Thanks for me, that’s it for this post but keep scrolling to see some examples of implementations from other languages.
1 2 3 4 |
var exampleArray = JSLINQ(myList) .Where(function(item){ return item.FirstName == "Chris"; }) .OrderBy(function(item) { return item.FirstName; }) .Select(function(item){ return item.FirstName; }); |
1 2 3 4 5 |
Iterable<? extends Number> r = from(data, where( { Integer i => i > 5 }, orderBy( { Integer i1, Integer i2 => i1-i2 }, select( { Number i => 4 } )))); |
go-linq
1 2 3 4 5 6 7 8 9 10 11 |
// Find students over 20 results, err := From(students). Where(func (s T) (bool, error){ return s.(*Student).age >= 20, nil }). // Select their ages. Select(func (s T) (T, error){ return s.(*Student).age, nil }). // Find the average. Average() |
Thanks for your time!