Overview

7 Designing programs with function composition

Function composition is presented as a core functional technique for building programs as pipelines of transformations. The chapter starts by defining composition mathematically and showing how one function’s output becomes another function’s input, then contrasts this with C#’s method chaining, which expresses the same idea more readably. Using small examples such as generating an email address, it shows that extension methods make composed operations appear in execution order, which improves clarity and makes workflows easier to follow.

The discussion then broadens to data flow and composable APIs such as LINQ, where operations like sorting, taking, projecting, and aggregating can be chained into a concise, declarative query. From there, the chapter identifies traits that make functions easier to compose: purity, generality, chainability, and shape preservation. It also emphasizes that good compositional design makes refactoring easier, because complex functions can be broken into smaller, reusable pieces without changing behavior, especially when working with functors like Option and obeying the relevant laws.

Finally, the chapter applies these ideas to a realistic server-side workflow for validating and processing a money transfer. Instead of using nested conditionals and imperative statements, the workflow is rewritten as a sequence of expressions built with Option, Map, Where, Bind, and ForEach, so validation, account lookup, debiting, persistence, and SWIFT wiring fit into a readable pipeline. Along the way, the chapter introduces functional domain modeling, separates data from behavior, and argues that higher-level components should orchestrate lower-level pure functions, leaving side effects to the end of the workflow where they are easiest to isolate and test.

Data flowing through a sequence of functions
Data flow through the AverageEarningsOfRichestQuartile function
Example workflow: validating a request before processing it
Viewing validation as a step in the data flow
A type mismatch preventing function composition
Modeling a linear workflow with function composition
The Option API helps us compose existing functions.
An unhelpful way to structure interaction between layers
A top-level workflow composing functions exposed by lower-level components
Mid-level components can be pure.

Summary

  • Function composition means combining two or more functions into a new function, and it’s widely used in FP.
  • In C#, the extension method syntax allows you to use function composition by chaining methods.
  • Functions lend themselves to being composed if they are pure, chainable, and shape-preserving.
  • Workflows are sequences of operations that can be effectively expressed in your programs through function pipelines: one function for each step of the workflow with the output of each function fed into the next.
  • The LINQ library has a rich set of easily composable functions to work with IEnumerables, and you can use it as inspiration to write your own APIs.
  • Functional code prefers expressions over statements, unlike imperative code.
  • Relying on expressions leads to your code becoming more declarative and, hence, more readable.

FAQ

What is function composition in Chapter 7?Function composition is the process of combining two functions so that the output of one becomes the input of the next. If h = f · g, then applying h to x is the same as computing f(g(x)).
Why does the chapter recommend method chaining over direct function composition in C#?C# does not have special syntax for function composition, so chained extension methods are usually easier to read. Method chaining shows the operations in the order they are executed, which is clearer than nested function calls.
What is the difference between function composition and method chaining?Function composition expresses a pipeline like f(g(x)), where execution order is read from the inside out. Method chaining uses the . operator to write the steps in execution order, such as joe.AbbreviateName().AppendDomain().
Why must composed functions have matching types?The output type of the inner function must be assignable to the input type of the outer function. If the types do not line up, the functions cannot be composed directly.
Why are extension methods useful for composing functions?Extension methods let you write functions in a chainable style. By making a function an extension method with a this parameter, you can call it as if it were part of the data flow.
What misconception about extension methods does the chapter warn against?The chapter warns not to think of extension methods as if they truly belong to the data type they extend. They are still just functions; the this syntax is mainly for readability.
How does LINQ demonstrate function composition?LINQ methods like OrderByDescending, Take, Select, and Average can be chained into a linear workflow. Each step transforms the data and passes it to the next step.
What makes a function more composable?Composable functions are usually pure, chainable, general-purpose, and shape-preserving. Pure functions are especially valuable because they have no side effects and are easier to reuse in pipelines.
How does the chapter use Option to model a workflow?Option is used to represent whether a value is present or absent, and in the workflow it also represents whether validation succeeded. Some means the workflow can continue, while None stops further processing.
Why does the end-to-end transfer workflow use Map, Where, Bind, and ForEach?These functions let the workflow remain declarative and composable. Map transforms the request, Where filters by validation, Bind chains operations that return Option, and ForEach performs the final side effects like saving and wiring funds.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free