You are previewing Functional Programming in C#: Classic Programming Techniques for Modern Projects.

Functional Programming in C#: Classic Programming Techniques for Modern Projects

  1. Cover
  2. Title
  3. Copyright
  4. About the Author
  5. Credits
  6. Contents
  7. Introduction
  8. Part I : Introduction to Functional Programming
    1. Chapter 1 : A Look at Functional Programming History
      1. What Is Functional Programming?
      2. Functional Languages
      3. The Relationship to Object Oriented Programming
      4. Summary
    2. Chapter 2 : Putting Functional Programming into a Modern Context
      1. Managing Side Effects
      2. Agile Programming Methodologies
      3. Declarative Programming
      4. Functional Programming Is a Mindset
      5. Is Functional Programming in C# a Good Idea?
      6. Summary
  9. Part II : C# Foundations of Functional Programming
    1. Chapter 3 : Functions, Delegates, and Lambda Expressions
      1. Functions and Methods
      2. Reusing Functions
      3. Anonymous Functions and Lambda Expressions
      4. Extension Methods
      5. Referential Transparency
      6. Summary
    2. Chapter 4 : Flexible Typing with Generics
      1. Generic Functions
      2. Generic Classes
      3. Constraining Types
      4. Other Generic Types
      5. Covariance and Contravariance
      6. Summary
    3. Chapter 5 : Lazy Listing with Iterators
      1. The Meaning of Laziness
      2. Enumerating Things with .NET
      3. Implementing Iterator Functions
      4. Chaining Iterators
      5. Summary
    4. Chapter 6 : Encapsulating Data in Closures
      1. Constructing Functions Dynamically
      2. The Problem with Scope
      3. How Closures Work
      4. Summary
    5. Chapter 7 : Code Is Data
      1. Expression Trees in .NET
      2. Analyzing Expressions
      3. Generating Expressions
      4. .NET 4.0 Specifics
      5. Summary
  10. Part III : Implementing Well-known Functional Techniques in C#
    1. Chapter 8 : Currying and Partial Application
      1. Decoupling Parameters
      2. Calling Parts of Functions
      3. Why Parameter Order Matters
      4. Summary
    2. Chapter 9 : Lazy Evaluation
      1. What’s Good about Being Lazy?
      2. Passing Functions
      3. Explicit Lazy Evaluation
      4. Comparing the Lazy Evaluation Techniques
      5. How Lazy Can You Be?
      6. Summary
    3. Chapter 10 : Caching Techniques
      1. The Need to Remember
      2. Precomputation
      3. Memoization
      4. Summary
    4. Chapter 11 : Calling Yourself
      1. Recursion in C#
      2. Tail Recursion
      3. Accumulator Passing Style
      4. Continuation Passing Style
      5. Indirect Recursion
      6. Summary
    5. Chapter 12 : Standard Higher Order Functions
      1. Applying Operations: Map
      2. Map, Filter, and Fold in LINQ
      3. Standard Higher Order Functions
      4. Summary
    6. Chapter 13 : Sequences
      1. Understanding List Comprehensions
      2. A Functional Approach to Iterators
      3. Ranges
      4. Restrictions
      5. Summary
    7. Chapter 14 : Constructing Functions from Functions
      1. Composing Functions
      2. Advanced Partial Application
      3. Combining Approaches
      4. Summary
    8. Chapter 15 : Optional Values
      1. The Meaning of Nothing
      2. Implementing Option(al) Values
      3. Summary
    9. Chapter 16 : Keeping Data from Changing
      1. Change Is Good — not!
      2. False Assumptions
      3. Implementing Immutable Container Data Structures
      4. Alternatives to Persistent Data Types
      5. Summary
    10. Chapter 17 : Monads
      1. What’s in a Typeclass?
      2. What’s in a Monad?
      3. Why Do a Whole Abstraction?
      4. A Second Monad: Logging
      5. Syntactic Sugar
      6. Binding with SelectMany?
      7. Summary
  11. Part IV : Putting Functional Programming into Action
    1. Chapter 18 : Integrating Functional Programming Approaches
      1. Refactoring
      2. Writing New Code
      3. Finding Likely Candidates for Functional Programming
      4. Summary
    2. Chapter 19 : The MapReduce Pattern
      1. Implementing MapReduce
      2. Abstracting the Problem
      3. Summary
    3. Chapter 20 : Applied Functional Modularization
      1. Executing SQL Code from an Application
      2. Rewriting the Function with Partial Application and Precomputation in Mind
      3. Summary
    4. Chapter 21 : Existing Projects Using Functional Techniques
      1. The .NET Framework
      2. LINQ
      3. Google MapReduce and Its Implementations
      4. NUnit
      5. Summary
  12. Index
O'Reilly logo

GENERATING EXPRESSIONS

Making the step from analyzing expressions to constructing them isn’t difficult. With the help of the ExpressionDumper and the support of the C# compiler (as far as it currently goes), it’s easy to find out how common expressions are represented in the trees. The Expression class has a lot of helper functions that make it easy to construct new expression trees at runtime. These can then be used in any APIs that use function passing (by compiling the expression trees) or in those that use the expression trees directly (like LINQ). This can be a modern form of dynamic querying, using expression trees as an intermediate step and thereby increasing flexibility, security, and type safety.

Here’s the expression tree again for the add function:

LambdaExpression (

  Parameters:

    ParameterExpression (x)

    ParameterExpression (y)

  Body:

    BinaryExpression:Add (

      Left:

        ParameterExpression (x)

      Right:

        ParameterExpression (y)

    )

)

Such a tree makes it easy to see the elements that are being used, and with the helper functions in the Expression class it’s just as easy to construct the same tree. Here’s the code for this function:

ParameterExpression xParam = Expression.Parameter(typeof(int), "x");

ParameterExpression yParam = Expression.Parameter(typeof(int), "y");

 

var addExpr = Expression.Lambda<Func<int, int, int>>(

  Expression.Add(xParam, yParam), xParam, yParam);

Starting from the innermost element, Expression.Add is used ...

The best content for your career. Discover unlimited learning on demand for around $1/day.