Related News
Functional Style Regex Engine in C# Revisited
In January 2007 I posted about a Functional Style Regex Engine in C#. This was an exercise in using iterators and anonymous functions in C#. I decided it was time to see if it was possible to rewrite the code using Linq.To recap, the goal is to be able to specify a regular expression in a functional style like this:// c(a|d)+rRgx.Expr e = Rgx.Seq(Rgx.Char('c'), Rgx.Seq(Rgx.Plus(Rgx.Alt(Rgx.Char('a'), Rgx.Char('d'))), Rgx.Char('r')));foreach (string r in e("cdar")) Console.WriteLine("Match with remainder: {0}", r);Calling an instance of Expr returns an enumeration of all the matches which start at the beginning of the input string. When each of the functions is called it returns a lambda expression which takes a string parameter. The expression returns an enumeration containing the remainder for every match on the string. For example, if the lambda expression generated by Rgx.Char('x') is passed the string "xyz", the enumeration returned by the expression will contain the single string "yz", this being the remainder from the match on the character 'x'.The rewritten class turns out to be quite neat compared to the original version which required some hackiness because the yield statement cannot be used in an anonymous block. Here it is:using System;using System.Collections.Generic;using System.Linq;class Rgx{ public delegate IEnumerable<string> Expr(string s); static Expr Nil() { return s => Enumerable.Repeat(s, 1); } static public Expr Seq(Expr l, Expr r) { return s => l(s).SelectMany(x => r(x)); } static public Expr Alt(Expr l, Expr r) { return s => l(s).Concat(r(s)); } static public Expr Star(Expr e) { return s => Nil()(s).Concat(Seq(e, Star(e))(s)); } static public Expr Plus(Expr e) { return Seq(e, Star(e)); } static public Expr Char(char c) { return s => s.Length > 0 && s[0] == c ? Enumerable.Repeat(s.Substring(1), 1) : Enumerable.Repeat("", 0); }}The implementation of Seq() made me think the most. The enumeration of match remainders from the left-hand side of the sequence is processed by the right-hand side of the sequence by using the SelectMany() function, i.e. each string in the output from the left-hand side is passed to the expression on the right-hand side, each call to the right-hand expression resulting in zero or more match remainders.Again, this is definitely not the way to implement regular expression matching, but just an exercise in using Linq.
Linq and Functional Programming
I'm writing some code to display some old blog posts and after retrieving each post as a single string containing CR-LF separated lines I needed to split the string into individual lines and wrap each line with a <p> tag. I could split the string using String.Split but I wondered if it was possible to use a StringReader to generate an enumeration of the lines in the string. It turns out that StringReader doesn't support this but I wrote an extension method to supply the required functionality:public static partial class Extensions{ public static IEnumerable<string> Lines(this TextReader textReader) { string line; while ((line = textReader.ReadLine()) != null) yield return line; }} The extension method is defined for the TextReader parent class of StringReader so it will also work for StreamReader, which makes it possible, for example, to generate an enumeration of the lines in a file (you could use File.ReadAllLines but that generates the array of every line in the file when it is called, whereas using an iterator means that data is read from the file as required for each yield statement).I also needed to concatenate the strings in the sequence of lines so I wrote another extension method:public static partial class Extensions{ public static string Concatenate(this IEnumerable<string> strings, string separator) { StringBuilder strbldr = new StringBuilder(); foreach (string str in strings) { if (strbldr.Length > 0) strbldr.Append(separator); strbldr.Append(str); } return strbldr.ToString(); }}This then allows me to write code like this:string txt = @"onetwothree";StringReader rdr = new StringReader(txt);string output = rdr.Lines() .Where(line => line != "") .Select(line => "<p>" + line + "</p>") .Concatenate(Environment.NewLine);I am finding that with the influence of Linq I am using a more functional style of coding, not just for manipulating data in a database but also for in-memory objects such as arrays. Treating an array as a sequence to which you can apply functions to means you can write higher level code which is easier to understand, and which is less likely to have bugs, because the code is focused on the required functionality rather than how to implement it. Bill Venners in How Scala Changed My Programming Style describes a similar experience. His example translates to C# as follows: the imperative version:var nameHasUpperCase = false; for (int i = 0; i < name.Length; i++){ if (char.IsUpper(name[i])) { nameHasUpperCase = true; break; }}And the functional version:var nameHasUpperCase = name.Any(c => char.IsUpper(c));Of course, as Raganwald says in Why Why Functional Programming Matters Matters, speaking of how functional code expresses a lot more what and a lot less how, this doesn't come for free:In general, we think this is a good thing. But it isn't free: somewhere else there is a mass of code that supports your brevity. When that extra mass of code is built into the programming language, or is baked into the standard libraries, it is nearly free and obviously a Very Good Thing. A language that doesn't just separate the concern of how but does the work for you is very close to "something for nothing" in programming.In the case of the code above I had to write the extension methods but in a more functionally oriented language it might not be necessary to write anything extra. In general, I wonder if a language designed to be inherently more functional, such as F#, is worth learning; not necessarily as a language for day-to-day use — it may be sometime before it achieves widespread commercial usage, if ever — but because it might feed back into using C# more effectively, moving towards a more functional style of coding where possible.
TestMaker 5.2 (Default branch)

PushToTest TestMaker is a platform for real-time monitoring and governance of information systems. Software developers use TestMaker turn their unit tests into functional tests that run on their development machine. TestMaker includes Wizards and Recorders to automatically build tests and supports a variety of languages to build tests, including Java, .NET, Jython, Groovy, PHP, Ruby, and many others. It supports SOA, Web Service, AJAX, and REST services using HTTP, HTTPS, SOAP, XML-RPC, and the email protocols. The TestMaker test runtime environment automatically turns these same functional tests into load tests, scalability and performance tests, regression tests, and service monitors for QA technicians, IT operations managers, and CIOs.License: GNU General Public License (GPL)Changes:This release has a new results analysis engine, Selenium IDE/RC for AJAX testing, multiple target monitoring, Web debugging utilities, new tutorials, and new data production libraries.

Programming in Scala: A Comprehensive Step-by-step Guide (Paperback) newly tagged “programming”

Programming in Scala: A Comprehensive Step-by-step Guide (Paperback)By Martin Odersky Buy new: $31.4931 used and new from $26.49 Customer Rating:

First tagged "programming" by Gary W. Fong "gwfong" Customer tags: scala(15), functional programming(10), programming languages(8), object oriented(7), java(6), concurrent programming(5), programming(3), erlang, haskell
Systemantics: The Underground Text of Systems Lore (Paperback) newly tagged “programming”

Systemantics: The Underground Text of Systems Lore (Paperback)By John Gall 10 used and new from $57.17 Customer Rating:

First tagged "programming" by William J. Romanos "Bill Romanos, III" Customer tags: software development, agile, functional programming, lisp, pragmatic programmer, marketing, programming, project management, computer science, haskell, common lisp