Functional Programming
Back to TOCJust VideosCondensed TOC
Language Integrated Query, when used properly, enables you to build powerful applications in the functional programming style. Pure functional transformations are my recommended approach to document manipulation.
Functional Programming (FP) has the potential to reduce program line count by 20% to 50%, reduce bugs and increase robustness, and move us in the direction of taking advantage of multiple core CPUs. But perhaps due to the learning curve, and unfamiliarity, many developers are not taking advantage of the potential that LINQ and FP offers. (Article)
Keywords: Functional Programming
One of the most important aspects of LINQ (and LINQ to XML) is that performance of chained queries can be effectively the same as if you write one larger, more complicated query. (Article)
XName and XNamespace objects are atomized. This yields performance benefits for queries: comparing two atomized names for equality means that the underlying intermediate language (IL) only needs to determine if the two references point to the same object. The underlying system does not need to do any string comparisons, which would be time consuming. (Article)
One approach that you can take to gain some performance is to pre-atomize XName objects. This would yield performance benefits when creating a large XML tree where specific names are repeated. The approach is to declare and initialize XName objects before the construction of the XML tree, and then to use the XName objects rather than specifying strings for the element and attribute names. (Article)
One of the most important performance benefits that you will note when comparing LINQ to XML and XmlDocument is that queries in LINQ to XML are statically compiled, whereas XPath queries must be interpreted at runtime. (Article)
There are a number of issues and dynamics that we can talk about with regards to performance of LINQ to XML. (Article)
This post introduces a GroupAdjacent generic extension method that groups elements in a collection with adjacent elements based on a common key. (Article)
Sometimes you have flat data where there is hierarchy expressed in the data, but the form of the data is flat. You may need to transform this data into a hierarchy, such as an XML tree. (Article)
Lists some important posts about LINQ to XML that explains approaches that you can take to write LINQ to XML code that has fewer bugs. (Article)
I had an interesting conversation with my nephew the other day. He is a very bright CS student working as a summer intern at a software company (not Microsoft). He is programming in C# using Visual Studio 2008. I asked him if developers at his company were using LINQ, and he said, “No, that the folks in charge had basically forbidden it, because no one understands it.” (Article)
There are four basic scenarios where developers use LINQ. (Article)
Shows how to write code to generate an XML tree from a hierarchy of anonymous types. In addition, it compares and contrasts writing code in the imperative style to writing code in the declarative style. (Article)
Sometimes you need to group a collection into a set of groups, each with n members. (Article)
You can use lambda expressions to write an event handler, even for classes that predate C# 3.0 and the latest version of the framework. This can shorten your code, and make it easier to read. (Article)
Sometimes you need to find the duplicates in a list. (Article)
Closures are one of the key components in C# 3.0 that makes functional programming easy, and results in clean syntax. (Article)
LINQ is a great tool for writing ad-hoc queries and transforms, and occasionally I need to write queries or transforms on text files. (Article)
Over time, I’ve adjusted my code formatting style – changing the white space that I insert, or where I place the parentheses. In this post, I detail some aspects of my current code formatting approach, focusing only on formatting LINQ queries that are written in the ‘method syntax’, not using LINQ query expressions (from — select —). (Article)
You may optionally be making a number of modifications to a very large XDocument object. Because of a complicated algorithm, you may not necessarily know ahead of time whether you will be making changes. If you don’t make any changes, then you don’t want to unnecessarily serialize the tree and save it to disk, or send it over the wire. This post presents a simple technique to determine if an XDocument has ever been modified since deserialization or creation. (Article)
Provides an overview of the similarities and differences between XPath and LINQ to XML (Article)
Anders Hejlsberg presented a fascinating and entertaining session at PDC 2008 on C# 4.0. He talked about dynamic typing, optional and named parameters, improved COM interoperability, and improved support for co-variance and contra-variance. These language improvements are very cool – they enable us to write code that more closely expresses our intent, with less syntactic noise. (Article)
Debugging LINQ queries can be problematic. One of the reasons is that quite often, you write a large query as a single expression, and you can’t set a breakpoint mid-expression. Writing large queries in expression context is particularly powerful when using functional construction to form XML (or using the strongly typed DOM in Open XML SDK V2). This post presents a little trick that makes it easier to use the debugger with LINQ queries that are written using ‘method syntax’. (Article)
When writing queries, just as you sometimes want to skip the first n items in a collection, on occasion you want to skip the last n items. You could certain count the items remaining in the collection, and use the Take operator to take all but the last, but this would mean iterating the collection twice, and would result in uglier code. (Article)
Functional Programming (FP) has the potential to reduce program line count by 20% to 50%, reduce bugs and increase robustness, and move us in the direction of taking advantage of multiple core CPUs. But perhaps due to the learning curve, and unfamiliarity, many developers are not taking advantage of the potential that LINQ and FP offers. (Article)
Keywords: Functional Programming
In certain scenarios, it is important to be able to compare two XML trees for equivalence. For example, if you are writing a web service that serves results of queries, and you want to cache query results so that duplicate queries use previously cached results instead of always accessing the underlying database. However, the senders of those queries may potentially be using a variety of tools to generate the queries, and these tools may introduce trivial differences into the XML. (Article)
There are a variety of circumstances where you want to clone a LINQ to XML tree while making modifications to the cloned tree. It’s possible to write a very small amount of recursive code to do this. (Article)
This post demonstrates the thought process of developing a somewhat more involved query. (Article)
I believe that it is easier to maintain code that is written in the functional style. For one thing, this is the very reason for many of the characteristics of functional code. No state is maintained, so we don’t have to worry about corrupting any state. If a variable is in scope, then the variable has its value, and it will never have another value. And the idea of composability is all about being able to inject/surround/refactor code without making it brittle. In this post, I’m going to show the process of maintaining and modifying a somewhat more involved query. (Article)
In some scenarios, it is useful to compute a ‘deep’ hash code using LINQ to XML. If you are writing a server-side application that caches queries expressed in XML, then you may want to store these queries in a hash table. (Article)
In two previous posts, I developed a somewhat involved query to search through a word processing document for style names and/or paragraph content. This is a query that I’m developing for the PowerTools for Open XML project. In those posts, as I evolved the query, I showed each iteration of it, highlighting the changes I made. This post continues modifying that query. (Article)
In my first version of my functional programming tutorial, I discussed a ForEach extension method, but I removed the topic from the second version. I no longer recommend using a ForEach extension method. This post explains why. (Article)
Writing pure functional transformations a in a recursive style enables us to put together interesting transformations in a very small amount of code. Using some specific techniques that allow us to write this code very concisely, this approach takes advantage of some perhaps obscure semantics of LINQ to XML. (Article)
Some XML vocabularies implement a powerful XML pattern that is analogous to inheritance in programming language type systems. Open XML WordprocessingML has these semantics around styles. When you define a new style, you can base this style on another style, and if the new style doesn’t explicitly define some aspect of the style, the new style ‘inherits’ this aspect from its base style. This post presents some code that uses one approach for implementing inheritance semantics. (Article)
This post introduces the Rollup extension method, which enables you to write LINQ code to produce a running total. In addition, it enables you to write the equivalent of a state machine using LINQ and Functional Programming. (Article)
Presents a small idiom to concatenate a set of strings. (Article)
Presents a bit of LINQ code to do a hex dump of a byte array. (Article)
Recursive descent parsers are one of the easier types of parsers to implement. Given a properly defined grammar, you write a class for each production in the grammar, and you write one fairly simple method in each class. This post is the first in a series on using LINQ to write a recursive-descent parser for SpreadsheetML formulas. (Article)
Keywords: Functional Programming
A grammar is a device to define syntax for a language. A grammar is made up of rules, sometimes called productions. Each rule defines a symbol, when can then be further used in other rules. Grammars are not hard to understand; most developers instinctively understand grammars when they see them. When you learn a new programming language, almost without thinking about it, you assemble some version of the grammar in your head. One of the benefits of reading the grammar of a language is to make sure that the conceptual grammar you’ve mentally assembled matches the actual grammar of the language. (Article)
Keywords: Functional Programming
To learn how recursive descent parsers work, it is helpful to implement a very simple grammar, so for pedagogical purposes, I’ve defined a grammar for simple arithmetic expressions. (Article)
Keywords: Functional Programming
A key operation when doing pure functional transformations is the process of creating complex hierarchies of objects. We see this when transforming some data source (such as an Open XML WordprocessingML document) to a LINQ to XML tree, and we see this when writing a recursive descent parser. (Article)
Keywords: Functional Programming
In this post, I present the start of a recursive descent parser that will parse the simple grammar that I presented previously in this series. I’ll point out some key features of the code so that it is easy to see how the code works. (Article)
Keywords: Functional Programming
In the process of developing the PowerTools for Open XML, over the years, I have developed some needed core functionality and utility methods, particularly for writing LINQ to XML code that queries and transforms Open XML documents. (Video)
Screen-cast that introduces a number of OpenXML specific utility methods and classes in PowerTools for Open XML. These classes and methods are in the PtOpenXmlUtil.cs module. (Video)