Concepts and Terminology (Functional Transformation)
This topic introduces the concepts and terminology of pure functional transformations. The functional transformation approach to transforming data yields code that is often quicker to program, more expressive, and easier to debug and maintain than more traditional, imperative programming.
Note that the topics in this section are not intended to fully explain functional programming. Instead, these topics identify some of the functional programming capabilities that make it easier to transform XML from one shape to another.
What Is Pure Functional Transformation?
In pure functional transformation, a set of functions, called pure functions, define how to transform a set of structured data from its original form into another form. The word "pure" indicates that the functions are composable, which requires that they are:
Self-contained, so that they can be freely ordered and rearranged without entanglement or interdependencies with the rest of the program. Pure transformations have no knowledge of or effect upon their environment. That is, the functions used in the transformation have no side effects.
Stateless, so that executing the same function or specific set of functions on the same input will always result in the same output. Pure transformations have no memory of their prior use.
Important
In the rest of this tutorial, the term "pure function" is used in a general sense to indicate a programming approach, and not a specific language feature.
Note that pure functions must be implemented as methods in C# and functions in Visual Basic. (In Visual Basic, subroutines cannot return a value, and therefore they are not used for writing pure functional transformations.)
Also, you should not confuse pure functions with pure virtual methods in C++. The latter indicates that the containing class is abstract and that no method body is supplied.
Functional Programming
Functional programming is a programming approach that directly supports pure functional transformation.
Historically, general-purpose functional programming languages, such as ML, Scheme, Haskell, and F# (from Microsoft Research), have been primarily of interest to the academic community. Although it has always been possible to write pure functional transformations in C# and Visual Basic, the difficulty of doing so has not made it an attractive option to most programmers. With C# 3.0 and Visual Basic 9.0, however, new language constructs such as lambda expressions and type inference make it functional programming much easier and more productive.
For more information about functional programming, see Functional Programming vs. Imperative Programming.
Domain-Specific FP Languages
Although general functional programming languages have not been widely adopted, specific domain-specific functional programming languages have had better success. For example, Cascading Style Sheets (CSS) are used to determine the look and feel of many Web pages, and Extensible Stylesheet Language Transformations (XSLT) style sheets are used extensively in XML data manipulation. For more information about XSLT, see XSLT Transformations.
Terminology
The following table defines some terms related to functional transformations.
higher-order (first-class) function
A function that can be treated as a programmatic object. For example, a higher-order function can be passed to or returned from other functions. In C# and Visual Basic, delegates and lambda expressions are language features that support higher-order functions. To write a higher-order function, you declare one or more arguments to take delegates, and you often use lambda expressions when calling it. Many of the standard query operators are higher-order functions.For more information, see Standard Query Operators Overview.
lambda expression
Essentially, an inline anonymous function that can be used wherever a delegate type is expected. This is a simplified definition of lambda expressions, but it is adequate for the purposes of this tutorial.For more information about, see Lambda Expressions (C# Programming Guide) or Lambda Expressions (Visual Basic).
collection
A structured set of data, usually of a uniform type. To be compatible with LINQ, a collection must implement the IEnumerable interface or the IQueryable interface (or one of their generic counterparts, IEnumerator<T> or IQueryable<T>).tuple (anonymous types)
A mathematical concept, a tuple is a finite sequence of objects, each of a specific type. A tuple is also known as an ordered list. Anonymous types are a language implementation of this concept, which enable an unnamed class type to be declared and an object of that type to be instantiated at the same time.For more information, see Anonymous Types (C# Programming Guide) or Anonymous Types (Visual Basic).
type inference (implicit typing)
The ability of a compiler to determine the type of a variable in the absence of an explicit type declaration.For more information, see Implicitly Typed Local Variables (C# Programming Guide) or Local Type Inference (Visual Basic).
deferred execution and lazy evaluation
The delaying of evaluation of an expression until its resolved value is actually required. Deferred execution is supported in collections.For more information, see Introduction to LINQ Queries (C#) and Deferred Execution and Lazy Evaluation in LINQ to XML.
These language features will be used in code samples throughout this section.
See Also
Concepts
Introduction to Pure Functional Transformations
Functional Programming vs. Imperative Programming