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

IMPLEMENTING OPTION(AL) VALUES

The basic structure of the class Option<T> is obvious: in addition to a value of type T, it stores a flag that says whether the value has been set. The type is immutable, so it is during construction that this decision is made: does the new instance represent an actual value or “nothing”?

It doesn’t make much sense to create loads of new instances of Option<T> that all represent “nothing” for any given type T. So the “nothing” case is covered by a single instance of the class, which is made available through a public field called None. Here’s what the class may look like at this point (this is not what is actually in FCSlib; read on for that):

public sealed class Option<T> {

  private readonly T value;

  public T Value {

    get { return value; }

  }

  private readonly bool hasValue;

  public bool HasValue {

    get { return hasValue; }

  }

  public bool IsSome {

    get { return hasValue; }

  }

  public bool IsNone {

    get { return !hasValue; }

  }

 

  public Option(T value) {

    this.value = value;

    this.hasValue = true;

  }

 

  private Option( ) {

  }

 

  public static readonly Option<T> None = new Option<T>( );

}

To create an option type instance, the code would look like this now:

var intVal = new Option<int>(42);

var intValNothing = Option<int>.None;

In both cases it is necessary to specify the actual value type explicitly because type inference doesn’t work in these scenarios. It is possible, though, to create a helper function to ...

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