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 using Visual Basic.

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

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


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]