Monday, October 12, 2009

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.]

One Really Big Query Expression!

No comments:

Post a Comment