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 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.
Presents the case for why you want to learn functional programming.
Functional programming is most applicable when transforming data.
Gives a quick overview of what the tutorial covers (and doesn't).
FP is about transformations. OOP is about class design and algorithms.
Presents example code to manipulate an (artificially simplified) image.
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.
Anonymous types are key to FP, and in certain circumstances, to use anonymous types, you must use local variable type inference.
To use anonymous types, you must use object and collection initializers.
A transformation is an operation of projection.
Tuples are important for creating intermediate values (and sometimes final results) of queries. This introduces tuples as implemented via anonymous types.
Nominal types allow for greater ease with refactoring.
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.
Aggregation is a core tool that you will use to develop results when using FP.
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.
Introduces the idea of declarative programming (vs. imperative programming).
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.
Shows the document that we'll be working with.
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.
Refactors the previously introduced query using a pure function. The resulting query is easier to read.
Extends the previous example to make it more correct.
Modifies the query to retrieve the text of each of the paragraphs. Adds the text to the tuple.
Introduces a new query operator, GroupAdjacent, implemented as an extension method on IEnumerable<T>.
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.
Contains the complete listing of our example.
Final thoughts about functional programming.