Friday, February 27, 2009 4:22 PM
Let’s Wax Functional 02/27/2009
This week I have a very diverse set of topics: First, using the functional programming concept of folds in C# to reimplement much of LINQ. Then a bit of reflection on F# Grammar Parsers. Finally, some community discussion on real world F# applications.
In this post Matthew reimplements much of LINQ using folds over IEnumerable. This post is a great opportunity for those who are living in the C# world to see how some of the constructs in the functional programming world work. I particularly like how Matt went the extra mile and included Pex tests for his examples. It gives extra clarity to his already well defined extension methods.
The basic idea behind a Parser Combinator is that you use it to build a parser with a large vocabulary out of a set of single vocabulary sub parsers. The claim is that, while this technique may not result in code that is as easy to read as something along the lines of fsyacc, it supports grammars which are much more complex while simultaneously allowing for an ultimately more extensible implementation.
I personally have not yet spent much time with either fsyacc or FParsec, but it’s interesting to contrast their different bottom up and top down approaches. Fsyacc seems ideal for parsing simple grammars such as SQL. The resulting code is very to understand and looks as though it would be similarly fast to write. However, if you wanted to implement something along the lines of regular expressions, FParsec seems a much better choice due to it’s direct support of infinite look-ahead.
A quick survey of Stack-Overflowers shows some uses cases for F# beyond DSL and Concurrency. My personal favorites are statistical calculations and visualizations as they can be a nightmare to deal with in C#. Also, as Talbott Crowell awed many with at the recent MSDN Boston conference, F# can be leveraged to easily make some amazing time-sensitive visualizations.
I was also surprised that more people weren’t using F# for testing or scripting as they are also both very good use cases.
A Note From Rick
I apologize for so frequently changing the name of my weekly roundup post. Over the last few months it’s moved more and more in the direction of FP in .NET and so I felt “Discoveries this Week” was no longer appropriate. Last week I decided to change the name but, after some reflection, it seems my choice was much too wordy. I think I’ve finally found a name that fits.