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.

More: continued here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Technorati

Related News


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


  • Programming in Scala: A Comprehensive Step-by-step Guide 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: 4.7 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


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


  • Systemantics: The Underground Text of Systems Lore (Paperback) newly tagged “programming”


  • Systemantics: The Underground Text of Systems Lore 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


  • Functional style regex engine in F#


  • Nick Palladinos has done a follow-up to my post Functional Style Regex Engine in C# Revisited. In his post Functional style regex engine in F# he presents just that, an F# version of my C# code:let char c (s : string) = seq { if s.Length > 0 && s.[0] = c then yield s.Substring(1) }let (=>) l r s = seq { for sl in l s do for sr in r sl -> sr }let (<|>) l r s = seq { yield! l s; yield! r s }let rec (<*>) e s = seq { yield s; yield! (e => (<*>) e) s }let (<+>) e = e => (<*>) e// example c(a|d)+rlet pattern = char 'c' => (<+>) (char 'a' <|> char 'd') => char 'r' An interesting difference to my C# version is the use of custom operators, particularly the => and <|> infix operators, which make for a much nicer syntax. Compare the definition of the function pattern above to this:Rgx.Expr e = Rgx.Seq(Rgx.Char('c'), Rgx.Seq(Rgx.Plus(Rgx.Alt(Rgx.Char('a'), Rgx.Char('d'))), Rgx.Char('r')));It's also nice to be able to define functions without having to put them in a class.


  • Practical Ruby Projects: Ideas for the Eclectic Programmer (Books for Professionals by Professionals) (Paperback) newly tagged “programming”


  • Practical Ruby Projects: Ideas for the Eclectic Programmer (Books for Professionals by Professionals) Practical Ruby Projects: Ideas for the Eclectic Programmer (Books for Professionals by Professionals) (Paperback)By Topher Cyll Buy new: $44.99$29.6950 used and new from $22.03 Customer Rating: First tagged "programming" by Travis V Allison Customer tags: ruby(2), open source, programming languages, programming, apress, ruby programming language


    Leave a Reply

    You must be logged in to post a comment.