Welcome to MSDN Blogs Sign in | Join | Help
Query Composition using Functional Programming Techniques in C# 3.0

[Blog Map] 

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

3. What this Tutorial Covers

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?"

7. Lambda Expressions

Introduction to lambda expressions, their syntax, and their semantics. Lambda expressions make functional programming fun.

8. Extension Methods

 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.

16. Deferred Execution

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.

24. Using the Open XML SDK

The Open XML SDK makes it easier to work with Open XML documents.

25. Retrieving the Paragraphs

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.

32. The Final Results

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.

An Explanation of Closures.

[Blog Map]

Posted: Wednesday, October 04, 2006 1:00 PM by EricWhite
Filed under:

Comments

Jeff Perrin - Sexier Than You Are said:

Since everyone else does it, I figured I&#39;d start linking as well: Keith Ray on the downsides to mocking

# November 1, 2006 10:26 PM

Small place on earth said:

Pentru cei interesaţi de LINQ, Eric White, developer în echipa de XLINQ a scris o serie de articole în

# January 7, 2007 4:37 PM

Lazy Coder said:

This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries....

# January 11, 2007 3:39 PM

Writing about Programming, Programming about Writing said:

Here are a few interesting blog posts. Eric White's Functional Programming Tutorial is a few months old

# February 15, 2007 12:44 AM

Wriju's BLOG said:

I found a very nice link on C# 3.0 resources and sharing it for you. Query Composition using Functional

# April 12, 2007 5:26 PM

Adrian. said:

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 need

# April 13, 2007 12:17 AM

Chua Wen Ching said:

Very good stuff. Keep it up :)

# September 27, 2007 5:34 AM

Dragan Panjkov said:

Eric,

can you publish this entire tutorial as pdf file? It is excellent (on first look)!

Thanks!

# February 13, 2008 9:03 PM

Steve said:

I agree that a PDF version would be rockin! :)

# April 24, 2008 9:00 AM

EricWhite said:

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.  :)

-Eric

# April 25, 2008 7:40 AM

Dragan Panjkov said:

I still agree that PDF version would be highly appreciated...

# April 25, 2008 8:13 AM

Dariusz quatscht said:

Hier ein Link zu einem interessanten Blog Eintrag: Query Composition using Functional Programming Techniques

# May 2, 2008 6:10 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 14, 2008 4:30 PM

Sam said:

These are awesome--great link post :)

# May 19, 2008 6:15 PM

www.cub-e.net said:

Query Composition using Functional Programming Techniques in C# 3.0

# July 26, 2008 4:05 AM

Eric White's Blog said:

I had an interesting conversation with my nephew the other day. He is a very bright CS student working

# July 29, 2008 12:44 AM

shahzad sarwar said:

Really a Great article, Very rich in thoery n concepts.... thanks for it ...

# August 7, 2008 6:52 AM

shahzad sarwar said:

Secondly , i have to read each line many times to understand the backgroud..... Really a rich guide.

# August 7, 2008 6:54 AM

EricWhite said:

Thanks for the kind words, Shahzad.  I'm glad it's useful.  :)

# August 14, 2008 12:00 PM

Eric White's Blog said:

Many types of documents contain code, including API documentation, tutorials, specifications, technical

# September 8, 2008 3:33 PM

Eric White's Blog said:

Closures are one of the key components in C# 3.0 that makes functional programming easy, and results

# September 11, 2008 10:46 PM

Eric White's Blog said:

Transforming Open XML documents using XSLT is an interesting scenario, but before we can do so, we need

# September 29, 2008 9:07 AM

Eric White's Blog said:

When developing C# programs in the functional programming style, you often need to dump out a collection

# October 5, 2008 7:04 AM

rob said:

Hey man,

great blog, some really good information im booking marking this and coming back a lot more.

rob

# November 3, 2008 8:59 AM

Eric White's Blog said:

[Blog Map] Excel has a very cool feature where you can declare that a range of cells is a table. It is

# November 14, 2008 10:00 AM

Cesa said:

Very useful tutorial, thanks. It would be nice to have a printed version, is there any hope that there will be a pdf version?

# November 18, 2008 4:36 AM

Nick's .NET Travels said:

Still trying to get you head around Linq and wondering what all the interest in Functional Programming

# November 25, 2008 5:09 PM

Eric White's Blog said:

Visual Basic 9.0 added many language features that allow us to write in the functional style in a natural

# November 25, 2008 11:47 PM

LINQ in Action - LINQ Book & News said:

In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 "Beyond

# February 25, 2009 1:19 PM

Fabrice's weblog said:

In LINQ in Action , we discuss about the missing ForEach query operator. This is in Chapter 5 "Beyond

# February 25, 2009 1:21 PM

Eric White's Blog said:

In my first version of my functional programming tutorial , I discussed a ForEach extension method, but

# April 8, 2009 11:24 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker