My first experience with functional programming was a Lisp class I took around ’92 or ’93. I thought all those parenthesis were irritating, but more than that, the slipperiness of the whole functional paradigm was – well, frustrating partly describes it, but gratuitous is another way I’d describe it. Who needed it?
Then began a happy interlude of 15 years of procedural/imperative programming – Ada, VB, C, C++, Java, C#, SQL, etc. Ahhhhh.
Now, functional programming is all the rage. You’ve got Scheme, Erlang, Haskell, etc. And when Linq came out, I’ve been seeing all these things like Dynamic Lambda Expressions, parse trees, Func<> delegates, etc. It’s like I’ve been tossed off the dock at age 6 all over again.
But having spent some time getting acquainted today, it’s not all that problematic. It’s a shift in perspective. And it definitely helps to have specific problems to solve. Case in point: trying to apply Linq Dynamic Extensions to be able to take a strongly-typed predicate like:
OrderBy(c => c.Name);
and turning it into something more dynamic, like:
…the better to be able to generate Linq results on the fly without having to paint yourself in a switch() corner. There’s a set of Dynamic Extensions available in this zip file that allows you to do just that sort of thing. Unusually for Microsoft, they don’t give you a step-by-step guide to actually implement it. The steps turn out to be pretty easy:
Copy the Dynamic.cs file to your project (I put it next to my .dbml file in my library project).
Add this statement to the class file in which you want to use the Dynamic Query functionality:
Then you can go ahead and so something like:
var p = (from s in Subject select s).OrderBy("name");
I’m pretty sure that when I get a free minute I’ll really enjoy getting into the functional programming theory in a more leisurely manner. Right now I have projects to get out the door, so I must be more workmanlike by necessity.