Archive

Archive for the ‘LINQ’ Category

Linq to C++0x

January 26, 2009 4 comments

Updated 2011-10-31Major update based on boost range adaptors!!

C++ has long had some functional-styled things in its standard library, in particular a header file called <functional>. But actually using it in a functional way has been painful, largely due to the lack of lambdas.

But C++0x (very good Wikipedia page, by the way) solves this, amongst many other long-standing problems.

With lambdas in place, the next thing I naturally wonder is: what about a complete system of lazy-evaluated list processing, which is to say: what about Linq to Objects in C++?

Read more…

Advertisements
Categories: C#, lambdas, LINQ Tags: , ,

Optimizing Aggregate for String Concatenation

December 10, 2008 5 comments

Linq lets you think in a very generally applicable way and solve a very wide variety of problems with a few key concepts. That’s a great thing. But it’s irritating when the elegant solution doesn’t perform as well as an ugly special case.

Using a combination of well-known Linq features I’m going to demonstrate that we already have the power to get the best of both worlds: speed and elegance.

One example that has always irked me (and which is simple enough to demonstrate the idea with) is this:

Enumerable.Range(0, size) 

Read more…

Categories: C#, expressions, lambdas, LINQ Tags: , , ,

Chain – a LINQ operator for dealing with Linked Lists

November 13, 2008 Leave a comment

I don’t think there’s anything in LINQ that will do this, though I expect I’m wrong – I have a tendency to write my own extension method to do something and then discover that LINQ already provides a general version of it. But in the mean time, here’s my Chain method:

Read more…

Categories: BCL, C#, LINQ, wish Tags: , , ,

AutoDisposal using PostSharp

November 12, 2008 1 comment

I complain to anyone who will listen about the poor language support in C# for the IDisposable interface. Yeah, we’ve got the using statement, and that’s fine as far as it goes.

But compare that to what C++/CLI has: the most complete support of any .NET language. Not just the equivalent of the using statement, but also automatic implementation of IDisposable that takes care of disposing of nested owned objects, including inherited ones, like magic (relatively speaking, unless you’re a C++ programmer in which case you’ve taken it for granted for a decade or two).

The relationship between deterministic clean-up (i.e. destructors) and garbage collection (i.e. finalizers) was quite vaguely understood until Herb Sutter clarified as part of his work on C++/CLI. But now it’s all very clear how it should work – the only problem is, there doesn’t seem to be any movement towards fixing it in any future version of C#.

Read more…

Categories: .NET, C#, IDisposable, LINQ Tags: , , ,

The Weirdness of Linq Query Expressions

November 6, 2008 Leave a comment

What does this bit of code do?

var result = from n in 5 where (n == 5) select (n + 1);

It looks completely hopeless. How can you loop through the contents of the number 5? The C# language reference says of the from clause:

The data source referenced in the from clause must have a type of IEnumerable, IEnumerable<T>, or a derived type such as IQueryable<T>.

But that is a lie!

The above code is translated into this:

var result = 5.Where(n => n == 5).Select(n => n + 1);

So in fact it is not necessary for the data source (the thing after the in keyword) to be enumerable at all. The only thing it must have is methods (or extension methods) called Where and Select. So let’s define them for all types:

Read more…

Categories: C#, LINQ Tags: ,