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

COMPOSING FUNCTIONS

The idea of composition is based on a very simple thought. Using an example of simple calculations, consider these lines of code:

int a = 10;

int b = a * 3;

int c = b + 27;

The first line assigns a value to a. The second line calculates b from a, by applying a calculation. Finally, the third line calculates c from b, again by applying a calculation. Instead of performing these calculations directly, you could create functions like this:

int CalcB(int a) {

  return a * 3;

}

 

int CalcC(int b) {

  return b + 27;

}

 

int a = 10;

int b = CalcB(a);

int c = CalcC(b);

In any real-world application, it is quite likely that you would have helper functions similar (in structure, not in the calculations themselves!) to CalcB and CalcC. Now imagine you have a particular algorithm somewhere in your application that starts with a values and needs to calculate c values from them — in other words, you’re not interested in b at all. There are several things you could do now. For instance, you could create a new function CalcCfromA:

int CalcCfromA(int a) {

  return a * 3 + 27;

}

That is not a very good idea because you might need the original functions elsewhere, and of course it’s generally a good guideline to separate functionality as much as possible, which leads to several small functions that each do just one thing. Instead, you could implement the function like this:

int CalcCfromA(int a) {

  return CalcC(CalcB(a));

}

This is much better, and you will see in a moment ...

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