Odd query expressions

Yesterday, I was proof reading chapter 11 of the book (and chapter 12, and chapter 13 – it was a long night). Reading my own text about how query expressions work led me to wonder just how far I could push the compiler in terms of understanding completely bizarre query expressions. Background Query expressions in C# 3 work by translating them into “normal” C# first, then applying normal compilation. So, for instance, a query expression of: from x in words where x.Length > 5 select x.ToUpper() … is translated into this: words.Where(x => x.Length > 5)      .Select(x => x.ToUpper()) Now, usually the source expression (words in this case) is something like a variable, … Continue reading Odd query expressions

Implementing deferred execution, and a potential trap to avoid

When talking about LINQ recently, I doodled an implementation of OrderBy on a whiteboard. Now, I know the real OrderBy method has to support ThenBy which makes life slightly tougher, but let’s suppose for a moment that it didn’t. Let’s further suppose that we don’t mind O(n2) efficiency, but we do want to abide by the restriction that the sort should be stable. Here’s one implementation: public static IEnumerable<TSource> OrderBy<TSource,TKey>    (this IEnumerable<TSource> source,     Func<TSource,TKey> keySelector){    IComparer<TKey> comparer = Comparer<TKey>.Default;    List<TSource> buffer = new List<TSource>();    foreach (TSource item in source)    {        // Find out where to insert the element –        // immediately … Continue reading Implementing deferred execution, and a potential trap to avoid

Data pipelines as a conceptual stepping stone to higher order functions

I was explaining data pipelines in LINQ to Objects to a colleague yesterday, partly as the next step after explaining iterator blocks, and partly because, well, I love talking about C# 3 and LINQ. (Really, it’s becoming a serious problem. Now that I’ve finished writing the main text of my book, my brain is properly processing the stuff I’ve written about. I hadn’t planned to be blogging as actively as I have been recently – it’s just a natural result of thinking about cool things to do with LINQ. It’s great fun, but in some ways I hope it stops … Continue reading Data pipelines as a conceptual stepping stone to higher order functions

Visualising the Mandelbrot set with LINQ – yet again

I’ve been thinking about ranges again, particularly after catching a book error just in time, and looking at Marc’s generic complex type. It struck me that my previous attempts were all very well, and demonstrated parallelisation quite neatly, but weren’t very LINQy. In particular, they certainly didn’t use LINQ for the tricky part in the same way that Luke Hoban’s ray tracer does. The thing is, working out the “value” of a particular point in the Mandelbrot set visualisation is actually quite well suited to LINQ: Start with a complex value Apply a transformation to it (square the current value, … Continue reading Visualising the Mandelbrot set with LINQ – yet again

LambdaExpression – source code would be nice

Just taking a quick break from proof-reading to post a thought I had yesterday. Visual LINQ uses ToString() to convert an expression tree’s body into readable text. In some cases it works brilliantly, reproducing the original source code exactly – but in other cases it’s far from useful. For instance, from this expression tree representation: (Convert(word.get_Chars(0)) = 113) I suspect you wouldn’t have guessed that the original code was: word[0] == ‘q’ Personally I don’t find it terrifically obvious, even though I can see how it works. Here’s a thought though – suppose the LambdaExpression class had a Source property … Continue reading LambdaExpression – source code would be nice

Visual LINQ: Watch query expressions as they happen!

Critical link (in case you can’t find it): Source Code Download Update: Dmitry Lyalin has put together a screencast of Visual LINQ in action – it gives a much better idea of what it’s like than static pictures do. There’s music, but no speech – so you won’t be missing any important information if you mute it.  I was going to save this until it was rather more polished, but I’ve just started reading the first proofs of C# in Depth, so it’s unlikely I’ll have much time for coding in the near future. I’m too excited about the results … Continue reading Visual LINQ: Watch query expressions as they happen!

My first screencast – automatic properties

I’ve recently been introduced to Dmitry Lyalin of Better Know a Framework. We’re getting together to do some screencasts, and the first one is now up. I’ll be embedding these on my C# in Depth site as well. The next one will be on object and collection initializers, then anonymous types. After that, we’ll see 🙂 Anyway, I’d be interested in comments. Things I know I want to do better: Use a headset to get less hum on the narration (and fewer clicks etc) Longer pauses between sections – they were recorded separately, but then put too close together There’s … Continue reading My first screencast – automatic properties

Human LINQ

Last night I gave a talk about C# 3 and LINQ, organised by Iterative Training and NxtGenUG. I attempted to cover all the features of C# 3 and the basics of LINQ in about an hour and a half or so. It’s quite a brutal challenge, and obviously I wasn’t able to go into much detail about anything. It went down reasonably well, but I can’t help feeling there’s a lot of room for improvement. That said, there was one part of the talk which really did go well, and made the appropriate points effectively. I had demonstrated the following … Continue reading Human LINQ

Language design, when is a language "done", and why does it matter?

As per previous posts, I’ve been thinking a fair amount about how much it’s reasonable to keep progressing a language. Not only have thoughts about C# 4 provoked this, but also a few other sources: Don Box on Ted Neward on Java (yes, two separate links – but view both) Don again on the “doneness” of XML The Channel9 video of Erik Meijer, Gilad Bracha and Mads Torgersen The video is very well worth watching in its entirety – even though I wouldn’t pretend to understand everything in it. (It’s worth watching rather than just listening to, by the way … Continue reading Language design, when is a language "done", and why does it matter?

C# 4, part 4: My manifesto and wishlist

The final part of this little series is the one where I suggest my own ideas for C# 4, beyond those I’ve already indicated my approval for in earlier posts. Before I talk about individual features, however, I’d like to put forward a manifesto which could perhaps help the decision-making process. I hasten to add that I haven’t run all the previous parts through this manifesto to make sure that I’ve been consistent, but all of these thoughts have been running around in my head for a while so I hope I haven’t been wildly out. Manifesto for C# 4 … Continue reading C# 4, part 4: My manifesto and wishlist