Query Composition using Functional Programming Techniques in C# 3.0
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.
This blog is inactive.
New blog: EricWhite.com/blog
Blog TOCThis is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries.
It is certainly possible to write simple LINQ queries without using these techniques, but as soon as you start writing more complicated queries, you need to understand these techniques.
Learning about functional programming made me a better and faster coder.
The tutorial is written in a granular fashion so that if you already understand a topic, you can skip it. Feel free, for instance, if you have firm grasp on lambda expressions and extension methods to skip those topics. At the end of each topic is a link to the next, or you can navigate using the table of contents.
1. Introduction to the FP Tutorial
Presents the case for why you want to learn functional programming.
2. The Main Scenario for Functional Programming
Functional programming is most applicable when transforming data.
Gives a quick overview of what the tutorial covers (and doesn't).
4. Contrast of FP to Traditional Object-Oriented Programming
FP is about transformations. OOP is about class design and algorithms.
5. An Example Presented in Both Coding Styles
Presents example code to manipulate an (artificially simplified) image.
6. Quick Intro to Query Expressions
Capsule summary of query expressions and explicit notation. Answers the question, "What is a query?"
Introduction to lambda expressions, their syntax, and their semantics. Lambda expressions make functional programming fun.
Introduction to extension methods, and their applicability to functional programming.
9. Local Variable Type Inference
Anonymous types are key to FP, and in certain circumstances, to use anonymous types, you must use local variable type inference.
10. Object and Collection Initializers
To use anonymous types, you must use object and collection initializers.
11. Projection
A transformation is an operation of projection.
12. Tuples and Anonymous Types
Tuples are important for creating intermediate values (and sometimes final results) of queries. This introduces tuples as implemented via anonymous types.
13. Using Nominal Types for Tuples
Nominal types allow for greater ease with refactoring.
14. The Yield Contextual Keyword
Yield blocks are the foundation on which lazy evaluation is based. Yield blocks were introduced to C# in version 2.0.
15. Lazy Evaluation
This introduces lazy evaluation, and in contrast, eager evaluation.
Deferred execution is related to lazy evaluation, but the two terms are not synonymous.
17. Aggregation
Aggregation is a core tool that you will use to develop results when using FP.
18. Grouping
Queries can project hierarchical result sets (in contrast to SQL, which projects only rectangular result sets).
19. Pure Functions
Refactoring in FP consists of creation of pure functions. This defines pure functions and tells why you want to use them.
20. Programming in a Functional Style
Introduces the idea of declarative programming (vs. imperative programming).
21. Parsing XML from an Open XML Document
This introduces our main task that we'll accomplish in this tutorial. This example pulls together all of the previously introduced topics, including lambda expressions, extension methods, tuples (including type inference and object initializers), lazy evaluation, aggregation, and pure functions.
22. The Source Open XML Document
Shows the document that we'll be working with.
23. Printing the Parts of an Open XML Document
Small utility program to extract the XML in parts so that you can see it more easily.
The Open XML SDK makes it easier to work with Open XML documents.
Develops a query that returns all of the paragraphs in a Word document. Creates an intermediate result using an anonymous type.
26. Coding Style
Shows a code formatting approach that sometimes yields more readable code.
27. Refactoring using a Pure Function
Refactors the previously introduced query using a pure function. The resulting query is easier to read.
28. Retrieving the Default Style Name from the Styles Part
Extends the previous example to make it more correct.
29. Retrieving the Text of the Paragraphs
Modifies the query to retrieve the text of each of the paragraphs. Adds the text to the tuple.
30. Retrieving the Two Code Groups
Introduces a new query operator, GroupAdjacent, implemented as an extension method on IEnumerable<T>.
31. Filtering Out the Groups that we Don't Want
Modifies the query to retrieve just the groups that we want.
Modifies the query to retrieve exactly what we want. This is the end target of this example.
33. Complete Listing of ParseWordML
Contains the complete listing of our example.
34. Conclusion
Final thoughts about functional programming.
Appendices
Using Annotations to Transform LINQ to XML Trees in the style of XSLT.
LINQ Reduces Line Counts and Makes Code "Pop".
Using LINQ to XML to Access Open XML Documents.
Comments
Anonymous
October 05, 2006
PingBack from http://blog.lab49.com/?p=648Anonymous
November 01, 2006
Since everyone else does it, I figured I'd start linking as well: Keith Ray on the downsides to mockingAnonymous
January 07, 2007
Pentru cei interesaţi de LINQ, Eric White, developer în echipa de XLINQ a scris o serie de articole înAnonymous
January 11, 2007
This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries....Anonymous
February 14, 2007
Here are a few interesting blog posts. Eric White's Functional Programming Tutorial is a few months oldAnonymous
April 12, 2007
I found a very nice link on C# 3.0 resources and sharing it for you. Query Composition using FunctionalAnonymous
April 12, 2007
This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries. It is certainly possible to write simple LINQ queries without using these techniques, but as soon as you start writing more complicated queries, you needAnonymous
September 26, 2007
Very good stuff. Keep it up :)Anonymous
February 13, 2008
Eric, can you publish this entire tutorial as pdf file? It is excellent (on first look)! Thanks!Anonymous
April 24, 2008
I agree that a PDF version would be rockin! :)Anonymous
April 24, 2008
I agree, a PDF would be good. However, for a variety of reasons, I can't do it right now. Hopefully in the not too far future. :) -EricAnonymous
April 25, 2008
I still agree that PDF version would be highly appreciated...Anonymous
May 01, 2008
Hier ein Link zu einem interessanten Blog Eintrag: Query Composition using Functional Programming TechniquesAnonymous
May 14, 2008
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
May 19, 2008
These are awesome--great link post :)Anonymous
July 25, 2008
Query Composition using Functional Programming Techniques in C# 3.0Anonymous
July 28, 2008
I had an interesting conversation with my nephew the other day. He is a very bright CS student workingAnonymous
August 06, 2008
Really a Great article, Very rich in thoery n concepts.... thanks for it ...Anonymous
August 06, 2008
The comment has been removedAnonymous
August 14, 2008
Thanks for the kind words, Shahzad. I'm glad it's useful. :)Anonymous
September 11, 2008
The comment has been removedAnonymous
September 29, 2008
Transforming Open XML documents using XSLT is an interesting scenario, but before we can do so, we needAnonymous
October 04, 2008
When developing C# programs in the functional programming style, you often need to dump out a collectionAnonymous
November 03, 2008
Hey man, great blog, some really good information im booking marking this and coming back a lot more. robAnonymous
November 17, 2008
Very useful tutorial, thanks. It would be nice to have a printed version, is there any hope that there will be a pdf version?Anonymous
November 25, 2008
The comment has been removedAnonymous
November 25, 2008
Visual Basic 9.0 added many language features that allow us to write in the functional style in a naturalAnonymous
February 25, 2009
In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 "BeyondAnonymous
February 25, 2009
In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 "BeyondAnonymous
November 24, 2009
Great tutorial Eric, will be super if pdf version is available.Anonymous
November 24, 2009
The comment has been removedAnonymous
April 21, 2011
The comment has been removedAnonymous
April 18, 2012
Hello Eric, Many thanks for that really helpful work, i can imagine the hours need to build that tutorial. Very interesting. And indeed a PDF can be a nice to have for me to.