liveBook · Manning

What you will learn in Functional Programming in C#, Second Edition

by Enrico Buonanno

Chapter by chapter

Chapter 1: Introducing functional programming

The introduction presents functional programming as a different way of thinking about programs from the mainstream imperative paradigm.

1.1 What is this thing called functional programming? · 1.1.1 Functions as first-class values · 1.1.2 Avoiding state mutation · 1.1.3 Writing programs with strong guarantees · 1.2 How functional a language is C#? · 1.2.1 The functional nature of LINQ · 1.2.2 Shorthand syntax for coding functionally · 1.2.3 Language support for tuples · 1.2.4 Pattern matching and record types · 1.3 What you will learn in this book · 1.4 Summary

What you learn in chapter 1 →

Chapter 2: Thinking in functions

The introduction defines higher-order functions and relates them to first-class functions and abstraction.

2.1 What’s a function, anyway? · 2.1.1 Functions as maps · 2.1.2 Representing functions in C# · 2.2 Higher-order functions (HOFs) · 2.2.1 Functions that depend on other functions · 2.2.2 Adapter functions · 2.2.3 Functions that create other functions · 2.3 Using HOFs to avoid duplication · 2.4 Exercises · 2.5 Summary

What you learn in chapter 2 →

Chapter 3: Why function purity matters

The introduction identifies pure functions as functions without side effects and previews their relevance to real-world programming.

3.1 What is function purity? · 3.1.1 Purity and side effects · 3.1.2 Strategies for managing side effects · 3.1.3 Avoid mutating arguments · 3.2 Enabling parallelization by avoiding state mutation · 3.2.1 Pure functions parallelize well · 3.2.2 Parallelizing impure functions · 3.2.3 Avoiding state mutation · 3.3 Purity and testability · 3.3.1 Isolating I/O effects · 3.3.2 A business validation scenario · 3.3.3 Why testing impure functions is hard · 3.4 Testing code that performs I/O · 3.4.1 Object-oriented dependency injection · 3.4.2 Testability without so much boilerplate · 3.5 Purity and the evolution of computing · 3.6 Exercises · 3.7 Summary

What you learn in chapter 3 →

Chapter 4: Designing function signatures and types

The introduction explains why function signature design and type design are closely connected in functional programming.

4.1 Designing function signatures · 4.1.1 Writing functions signatures with arrow notation · 4.1.2 How informative is a signature? · 4.2 Capturing data with data objects · 4.2.1 Primitive types are often not specific enough · 4.2.2 Constraining inputs with custom types · 4.2.3 Writing honest functions · 4.2.4 Composing values into complex data objects · 4.3 Modeling the absence of data with Unit · 4.3.1 Why void isn’t ideal · 4.3.2 Bridging the gap between Action and Func · 4.4 Summary

What you learn in chapter 4 →

Chapter 5: Modeling the possible absence of data

This introduction identifies optional data as a type-design problem and presents Option as the functional alternative to null.

5.1 The bad APIs you use every day · 5.2 An introduction to the Option type · 5.3 Implementing Option · 5.3.1 An idealized implementation of Option · 5.3.2 Consuming an Option · 5.3.3 Creating a None · 5.3.4 Creating a Some · 5.3.5 Optimizing the Option implementation · 5.4 Option as the natural result type of partial functions · 5.4.1 Parsing strings · 5.4.2 Looking up data in a collection · 5.4.3 The smart constructor pattern · 5.5 Dealing with null · 5.5.1 Why null is such a terrible idea · 5.5.2 Gaining robustness by using Option instead of null · 5.5.3 Non-nullable reference types? · 5.5.4 Bulletproof against NullReferenceException · 5.6 Exercises · 5.7 Summary

What you learn in chapter 5 →

Chapter 6: Patterns in functional programming

The introduction frames recurring functional operations as reusable patterns that apply across different structures.

6.1 Applying a function to a structure’s inner values · 6.1.1 Mapping a function onto a sequence · 6.1.2 Mapping a function onto an Option · 6.1.3 How Option raises the level of abstraction · 6.1.4 Introducing functors · 6.2 Performing side effects with ForEach · 6.3 Chaining functions with Bind · 6.3.1 Combining Option-returning functions · 6.3.2 Flattening nested lists with Bind · 6.3.3 Actually, it’s called a monad · 6.3.4 The Return function · 6.3.5 Relationship between functors and monads · 6.4 Filtering values with Where · 6.5 Combining Option and IEnumerable with Bind · 6.6 Coding at different levels of abstraction · 6.6.1 Regular vs. elevated values · 6.6.2 Crossing levels of abstraction · 6.6.3 Map vs. Bind, revisited · 6.6.4 Working at the right level of abstraction · 6.7 Exercises · 6.8 Summary

What you learn in chapter 6 →

Chapter 7: Designing programs with function composition

The introduction connects functional programming’s extensive use of composition with LINQ’s concise list-processing API.

7.1 Function composition · 7.1.1 Brushing up on function composition · 7.1.2 Method chaining · 7.1.3 Composition in the elevated world · 7.2 Thinking in terms of data flow · 7.2.1 Using LINQ’s composable API · 7.2.2 Writing functions that compose well · 7.3 Programming workflows · 7.3.1 A simple workflow for validation · 7.3.2 Refactoring with data flow in mind · 7.3.3 Composition leads to greater flexibility · 7.4 An introduction to functional domain modeling · 7.5 An end-to-end server-side workflow · 7.5.1 Expressions vs. statements · 7.5.2 Declarative vs. imperative · 7.5.3 The functional take on layering · 7.6 Exercises · 7.7 Summary

What you learn in chapter 7 →

Chapter 8: Functional error handling

This introduction contrasts exception-based error handling with representing success or failure as a returned value.

8.1 A safer way to represent outcomes · 8.1.1 Capturing error details with Either · 8.1.2 Core functions for working with Either · 8.1.3 Comparing Option and Either · 8.2 Chaining operations that may fail · 8.3 Validation: A perfect use case for Either · 8.3.1 Choosing a suitable representation for errors · 8.3.2 Defining an Either-based API · 8.3.3 Adding validation logic · 8.4 Representing outcomes to client applications · 8.4.1 Exposing an Option-like interface · 8.4.2 Exposing an Either-like interface · 8.4.3 Returning a result DTO · 8.5 Variations on the Either theme · 8.5.1 Changing between different error representations · 8.5.2 Specialized versions of Either · 8.5.3 Refactoring to Validation and Exceptional · 8.5.4 Leaving exceptions behind? · 8.6 Exercises · 8.7 Summary

What you learn in chapter 8 →

Chapter 9: Structuring an application with functions

This introduction explains how partial application specializes parameterized functions and previews its role in composing applications from functions.

9.1 Partial application: Supplying arguments piecemeal · 9.1.1 Manually enabling partial application · 9.1.2 Generalizing partial application · 9.1.3 Order of arguments matters · 9.2 Overcoming the quirks of method resolution · 9.3 Curried functions: Optimized for partial application · 9.4 Creating a partial-application-friendly API · 9.4.1 Types as documentation · 9.4.2 Particularizing the data access function · 9.5 Modularizing and composing an application · 9.5.1 Modularity in OOP · 9.5.2 Modularity in FP · 9.5.3 Mapping functions to API endpoints · 9.5.4 Comparing the two approaches · 9.6 Reducing a list to a single value · 9.6.1 LINQ’s Aggregate method · 9.6.2 Aggregating validation results · 9.6.3 Harvesting validation errors · 9.7 Exercises · 9.8 Summary

What you learn in chapter 9 →

Chapter 10: Working effectively with multi-argument functions

This introduction frames the challenge of using multi-argument functions with effectful types and recalls the effects those types represent.

10.1 Function application in the elevated world · 10.1.1 Understanding applicatives · 10.1.2 Lifting functions · 10.1.3 An introduction to property-based testing · 10.2 Functors, applicatives, and monads · 10.3 The monad laws · 10.3.1 Right identity · 10.3.2 Left identity · 10.3.3 Associativity · 10.3.4 Using Bind with multi-argument functions · 10.4 Improving readability by using LINQ with any monad · 10.4.1 Using LINQ with arbitrary functors · 10.4.2 Using LINQ with arbitrary monads · 10.4.3 The LINQ clauses let, where, and others · 10.5 When to use Bind vs. Apply · 10.5.1 Validation with smart constructors · 10.5.2 Harvesting errors with the applicative flow · 10.5.3 Failing fast with the monadic flow · 10.6 Exercises · 10.7 Summary

What you learn in chapter 10 →

Chapter 11: Representing state and change

This introduction contrasts modeling real-world change by mutating an existing object with representing each observation as a new value.

11.1 The pitfalls of state mutation · 11.2 Understanding state, identity, and change · 11.2.1 Some things never change · 11.2.2 Representing change without mutation · 11.3 Using records to capture the state of domain entities · 11.3.1 Fine-grained control on record initialization · 11.3.2 Immutable all the way down · 11.4 Separating data and logic · 11.5 Summary

What you learn in chapter 11 →

Chapter 12: A short introduction to functional data structures

This introduction motivates immutable data structures and explains why creating a changed collection need not mean copying every element.

12.1 The classic functional linked list · 12.1.1 Common list operations · 12.1.2 Modifying an immutable list · 12.1.3 Destructuring any IEnumerable · 12.2 Binary trees · 12.2.1 Common tree operations · 12.2.2 Structure sharing · 12.3 In conclusion · 12.4 Exercises · 12.5 Summary

What you learn in chapter 12 →

Chapter 13: Event sourcing: A functional approach to persistence

This introduction applies functional immutability to persisted data and contrasts assertion-based and event-based append-only storage.

13.1 Thinking functionally about data storage · 13.1.1 Why data storage should be append-only · 13.1.2 Relax and forget about storing state · 13.2 Event sourcing basics · 13.2.1 Representing events · 13.2.2 Persisting events · 13.2.3 Representing state · 13.2.4 Representing state transitions · 13.2.5 Reconstructing the current state from past events · 13.3 Architecture of an event-sourced system · 13.3.1 Handling commands · 13.3.2 Handling events · 13.3.3 Adding validation · 13.3.4 Creating views of the data from events · 13.4 Comparing different approaches to immutable storage · 13.4.1 Datomic vs. Event Store · 13.4.2 How event-driven is your domain? · 13.5 Summary

What you learn in chapter 13 →

Chapter 14: Lazy computations, continuations, and the beauty of monadic composition

This introduction presents composition independently of execution as a common idea behind the chapter’s approaches to computations.

14.1 The virtue of laziness · 14.1.1 Lazy APIs for working with Option · 14.1.2 Composing lazy computations · 14.2 Exception handling with Try · 14.2.1 Representing computations that may fail · 14.2.2 Safely extracting information from a JSON object · 14.2.3 Composing computations that may fail · 14.2.4 Monadic composition: What does it mean? · 14.3 Creating a middleware pipeline for DB access · 14.3.1 Composing functions that perform setup/teardown · 14.3.2 A recipe against the pyramid of doom · 14.3.3 Capturing the essence of a middleware function · 14.3.4 Implementing the query pattern for middleware · 14.3.5 Adding middleware that times the operation · 14.3.6 Adding middleware that manages a DB transaction · 14.4 Summary

What you learn in chapter 14 →

Chapter 15: Stateful programs and stateful computations

The introduction defines stateful programs by contrasting their behavior with stateless programs.

15.1 Programs that manage state · 15.1.1 Caching data in memory · 15.1.2 Refactoring for testability and error handling · 15.1.3 Stateful computations · 15.2 A language for generating random data · 15.2.1 Generating random integers · 15.2.2 Generating other primitives · 15.2.3 Generating complex structures · 15.3 A general pattern for stateful computations · 15.4 Summary

What you learn in chapter 15 →

Chapter 16: Working with asynchronous computations

This introduction explains the basic purpose of asynchrony and distinguishes a single asynchronous value from an asynchronous sequence.

16.1 Asynchronous computations · 16.1.1 The need for asynchrony · 16.1.2 Representing asynchronous operations with Task · 16.1.3 Task as a container for a future value · 16.1.4 Handling failure · 16.1.5 An HTTP API for currency conversion · 16.1.6 If it fails, try a few more times · 16.1.7 Running asynchronous operations in parallel · 16.2 Async streams · 16.2.1 Reading from a file as an async stream · 16.2.2 Consuming async streams functionally · 16.2.3 Consuming data from several streams · 16.2.4 Aggregation and sorting with async streams · 16.3 Summary

What you learn in chapter 16 →

Chapter 17: Traversable and stacked monads

This introduction connects familiar containers with their effects and motivates combining different containers.

17.1 Traversables: Working with lists of elevated values · 17.1.1 Validating a list of values with monadic Traverse · 17.1.2 Harvesting validation errors with applicative Traverse · 17.1.3 Applying multiple validators to a single value · 17.1.4 Using Traverse with Task to await multiple results · 17.1.5 Defining Traverse for single-value structures · 17.2 Combining asynchrony and validation (or any other two monadic effects) · 17.2.1 The problem of stacked monads · 17.2.2 Reducing the number of effects · 17.2.3 LINQ expressions with a monad stack · 17.3 Summary

What you learn in chapter 17 →

Chapter 18: Data streams and the Reactive Extensions

This introduction explains data streams and reactive programming through examples and identifies Reactive Extensions as a library set for working with IObservables.

18.1 Representing data streams with IObservable · 18.1.1 A sequence of values in time · 18.1.2 Subscribing to an IObservable · 18.2 Creating IObservables · 18.2.1 Creating a timer · 18.2.2 Using Subject to tell an IObservable when it should signal · 18.2.3 Creating IObservables from callback-based subscriptions · 18.2.4 Creating IObservables from simpler structures · 18.3 Transforming and combining data streams · 18.3.1 Stream transformations · 18.3.2 Combining and partitioning streams · 18.3.3 Error handling with IObservable · 18.3.4 Putting it all together · 18.4 Implementing logic that spans multiple events · 18.4.1 Detecting sequences of pressed keys · 18.4.2 Reacting to multiple event sources · 18.4.3 Notifying when an account becomes overdrawn · 18.5 When should you use IObservable? · 18.6 Summary

What you learn in chapter 18 →

Chapter 19: An introduction to message-passing concurrency

This introduction connects shared mutable state with concurrency hazards and defines agent-based concurrency in terms of message passing and state ownership.

19.1 The need for shared mutable state · 19.2 Understanding message-passing concurrency · 19.2.1 Implementing agents in C# · 19.2.2 Getting started with agents · 19.2.3 Using agents to handle concurrent requests · 19.2.4 Agents vs. actors · 19.3 Functional APIs, agent-based implementations · 19.3.1 Agents as implementation details · 19.3.2 Hiding agents behind a conventional API · 19.4 Message-passing concurrency in LOB applications · 19.4.1 Using an agent to synchronize access to account data · 19.4.2 Keeping a registry of accounts · 19.4.3 An agent is not an object · 19.4.4 Putting it all together · 19.5 Summary

What you learn in chapter 19 →

Concepts explained in this book

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.