I have been a bit quiet for the past two months on my blog, since I was doing a presentation at the code camp at the end up April. Well, I am doing a presentation at the upcoming June 5th Richmond .Net users group. For some reason when I get into working on my presentations (probably because I haven't done too many of them) I somewhat obsess over them. I write code and rewrite code, and google…google…google. I start becoming obsessed with exploring every little nook of something I am doing, and then in the end I still feel like I know nothing about my topic!
Well, my presentation coming up is on the features of C# 3.0 that have been borrowed from functional programming. It isn't a presentation about functional programming in C#, since C# is not a functional language. If you want to write functional code, write it in F#, you'll bang your head against the wall a lot less! I am mainly going to be talking about delegates, lambdas, closures, and higher order functions.
I am aiming this presentation at a intermediate level presentation, that is going to try to get at some more beginner content at the start. I don't want people who are not familiar with delegates and closures to be left out in the cold! Although I will assume that people at least know what delegates are, but I won't expect them to know what anonymous methods are or the fact that they are closures. I really want this presentation to be accessible, and I am worried that it is not going to be.
Closures, for some people (including me), can be some mind bending stuff. One of the things that I am going to do with closures is show how to do memoization. I feel like it is a very useful process, that can be explained in a somewhat simple manner. I am worried though that some people just won't be able to wrap their mind around it. So, I sincerely hope that I will be able to break it down enough for people to grok it. If I don't, I have failed.
On that note, I dropped currying and partial function application from my presentation. I just felt like their implementation in C# is extremely ugly, it is extremely complicated, and quite frankly its usefulness in a non-functional language is limited. It wasn't an easy decision, because mind bending stuff like that is just so exciting for me. Who knows why, I just love to look at a piece of code and say, what the heck does that do? Check it out…
public static Func<TArg1, Func<TArg2, TResult>>
Curry<TArg1, TArg2, TResult>
(Func<TArg1, TArg2, TResult> func)
{
return argument1 => argument2 => func(argument1, argument2);
}
I mean, just look at that. The first time I saw code like that I knew that an adventure was ready to being. It was like a kid staring through the front window of a candy store. And granted, once you understand it, some of the magic goes away but the adventure never ends. I am going to leave my currying code in my presentation project, just in case by some miracle we have some time at the end I can go into it. I doubt that will be the case though.
So, I am hoping that once my obsessing ends I am going to get back to doing some more blogging. I have a few great ideas for blog posts that came out of the Meet and Code dinner that we did the other night. So I can't wait to get them written up. I also have a few other ideas that I need to find time to get to. So, anyways, I hope that you can hold out for a few more days, and if you live in Richmond come see me talk on June 5th!
Loved the article? Hated it? Didn’t even read it?
We’d love to hear from you.
Great talk today Justin! I thought the presentation was totally accessible and the examples were very clear. I think even the folks that were already familiar with the new language features learned a bunch of new tricks. I know I did. It inspired me to go off and take a look at F# too. I think more talks like this are exactly what the .net usergroup needs.
Thanks!
Thanks! I’m glad you enjoyed it!
I’m loving the closure-maker function, very neat. I’ve been mucking around with DSLs lately to build up content pages. I want to go from, say, a table builder, to a row builder, to a cell builder and back out again to start on the next row. I’m not sure if the formatting will work in this comment box, but here goes:
TableBuilder builder = new TableBuilder();
builder
.NewRow()
.Cell("Text")
.Link("foo.com")
.Image("img.jpg")
.Back()
.NewRow()
.Cell("
… and so on.
So, just so you know, your closures snippet will help a bunch. But, looking at how c# is trying to mix type safety with dynamic language ideas, one wonders if type safety isn’t worth the schlep. I wish c# would let me strap methods onto objects as I need it, so that I don’t have to pass generics and delegates, and get wrapped up in an obscure class heirachy 🙂
Anyways, thanks for sharing the snippet.
Cheers,
Luke
Time will tell if type safety is a good thing or not. Some people will say that this argument is already over. I’m not 100% convinced of this. What I am convinced of is that dynamic languages work really well in certain types of code, and that statically typed languages work really well for other types of code.
Your html fluent interface looks pretty cool, are you considering plugging it in as a view engine in asp.net MVC?
Also, you might want to check out NHaml for ideas since they have already worked through the xhtml/dsl mapping.
Hey Justin,
I totally agree with your comment – I don’t think there’s a one-size-fits-all, but this is certainly bringing a lot of interest to the functional programming arena. I’m keen to play with erlang, I hear it’s dynamic & functional (two buzzwords makes it better 🙂 )
[quote]Your html fluent interface looks pretty cool, are you considering plugging it in as a view engine in asp.net MVC?[/quote]
Thanks, yep, how’d you guess it was aimed at MVC? hehehe. We’re building a restful web framework at my company, where most of us come from a winforms background. We want a "debuggable" and familiar-feeling way to build up content in ViewPages. PS: we found out about DebuggerVisualizers the other day, and so put one onto the underlying presentation objects. They rock: if you’re at a breakpoint, clicking the little magnifying glass on one of the objects will open a winform with an IE control, showing your rendered html content (without styling).
[quote]Also, you might want to check out NHaml for ideas since they have already worked through the xhtml/dsl mapping.[/quote]
I’ll check this out, thanks!
Yeah, debugging visualizers are pretty awesome. We looked at them at last months "meet and code" dinner. Although I use them quite frequently, especially the Linq expression tree visualizer that MS shipped in the VS2008 samples, I have never fully implemented one. Was there anything tricky about implementing it?