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

ADVANCED PARTIAL APPLICATION

Partial application was covered to some extent in Chapter 8, but the use cases that were described there were rather simple. In the overall context of function construction, partial application deserves a bit more attention.

To remind you, currying is the conversion step from a multi-parameter function to a chain of single-parameter functions. It enables partial application, the technique of passing less than the complete set of parameters to a function in curried format, in order to create new functions out of existing ones.

Chapter 12 showed the standard higher order functions, and applying partial application to these functions creates many interesting opportunities. These functions tend to receive an algorithm as one of their parameters, in the form of an anonymous function, which they continue to apply to a list of elements. Partial application makes it easy to create very useful helper functions on the basis of these higher order functions, by applying the algorithm first and leaving the data to work against for later. Here’s a simple example of a function to square all the integer values in a sequence:

var curriedMap =

  Functional.Curry<Converter<int, int>,

    IEnumerable<int>, IEnumerable<int>>(Functional.Map<int, int>);

 

var squareList = curriedMap(x => x * x);

 

var list = new int[] { 2, 3, 4 };

var squaredList = squareList(list);

foreach (var item in squaredList)

  Console.Write("{0} ", item);

Console.WriteLine( );

The curried Map function ...

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