Taking LINQ to Objects to Extremes: A fully LINQified RayTracer

Not too long ago I blogged about a C# raytracer which took advantage of a lot of C#3.0 language constructs. However, you may have noticed that it did not actually use LINQ query expressions all that much. Well, after discussing this with a coworker on the PLINQ team at lunch one day - I was convinced that it should be possible to express the logic of the raytracer in a LINQ to Objects query. The result is below - a single 60-line LINQ query which captures the complete raytracing algorithm.

[DISCLAIMER: I am by no means suggesting it's a good idea to write code like this. Do so only at your own risk! Just as you *could* write your entire application in one method body, or decide to replace all if statements with virtual method dispatches, doing what I show below is a clear abuse of this language construct. But it's an interesting example to show that there is a lot of expresiveness available in C#3.0 query expressions.]

Let Expressions

"let" query clauses allow you to introduce a variable into scope which can be used by the following query clauses. Similar to local variables in a method body, this gives you a way to avoid evaluating a common expression multiple times by storing it in a variable. This can be very useful even in much simpler queries. Of course, in the query above - let is absolutely critical.

Recursion

A raytracer is inhenertly recursive - whenever a ray hits a reflective or transparent surface, new rays have to be cast. Thus the query that expreses the raytracer needs to be recursive. But the let clause doesn't support declaring recursive functions. Not to let that stop us (as it really should!) - we can use the Y combinator to introduce recursion by hand. Mads wrote a very detailed description of the recursive lambda expressions in C# recently - and the code example above uses my personal implementation of some of the same ideas he describes.

Expression-based control structures

When you are trying to write things in a single query expression - you don't have the luxury of using any statements. Since most of the C# control structures are statement based (if, switch, for, while, etc.) - this can make things a little trickier. One really valauble expression-based control structure is the ternary operator ( x ? y : z ). This is used in a couple of places above to provide the necessary branching logic. The rest of the control structuring is accomplished with LINQ Standard Query Operators - Select, SelectMany, Where, OrderBy, Aggregate and DefaultIfEmpty. As functional programmers have shown for years, the expressiveness of these very general list processing operations is impressive.

Conclusion

In case you had any doubts - hopefully this convinces you that C#3.0 Query Expressions are really a very expressive language construct. Of course - this expressiveness is probably best used in more moderation than shown here!

There is a screenshot of the image produced by the raytracer on my previous blog post (linked to above). Probably more fun though to try it out for yourself :-). The attached code file can be compiled with the C#3.0 compiler at the command line or imported into a VS 2008 project.