Functional Programming with C#: Create More Supportable, Robust, and Testable Code 9781492097075

After decades of relative obscurity, functional programming is finally coming into its own. With concise, easy-to-read c

211 109 6MB

English Pages 325 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Functional Programming with C#: Create More Supportable, Robust, and Testable Code
 9781492097075

Table of contents :
Preface
Who Should Read This Book?
Why I Wrote This Book
Navigating This Book
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
1. Introduction
What Is Functional Programming?
Is It a Language, an API, or What?
The Properties of Functional Programming
Immutability
Higher-order functions
Expressions rather than statements
Expression-based programming
Referential transparency
Recursion
Seriously, recursion
Pattern matching
Stateless
Baking Cakes
An Imperative Cake
A Declarative Cake
Where Does Functional Programming Come From?
Who Else Does Functional Programming?
Pure Functional Languages
Is It Worth Learning a Pure Functional Language First?
What About F#? Should I Be Learning F#?
Multiparadigm Languages
The Benefits of Functional Programming
Concise
Testable
Robust
Predictable
Better Support for Concurrency
Reduced Code Noise
The Best Places to Use Functional Programming
Where You Should Consider Using Other Paradigms
How Far Can We Take This?
Monads Actually, Don’t Worry About This Yet
Summary
I. What Are We Already Doing?
2. What Can We Do Already?
Getting Started
Writing Your First Functional Code
A Nonfunctional Film Query
A Functional Film Query
Focusing on Results-Oriented Programming
Understanding Enumerables
Preferring Expressions to Statements
The Humble Select
Iterator value is required
No starting array
Many to One: The Subtle Art of Aggregation
Customized Iteration Behavior
Making Your Code Immutable
Putting It All Together: A Complete Functional Flow
Taking It Further: Develop Your Functional Skills
Summary
3. Functional Coding in C# 7 and Beyond
Tuples
Pattern Matching
Procedural Bank Accounts
Pattern Matching in C# 7
Pattern Matching in C# 8
Pattern Matching in C# 9
Pattern Matching in C# 10
Pattern Matching in C# 11
Read-Only Structs
Init-Only Setters
Record Types
Nullable Reference Types
The Future
Discriminated Unions
Active Patterns
Summary
4. Work Smart, Not Hard with Functional Code
It’s Time to Get Func-y
Funcs in Enumerables
A Super-Simple Validator
Pattern Matching for Old Versions of C#
Make Dictionaries More Useful
Parsing Values
Custom Enumerations
Query Adjacent Elements
Iterate Until a Condition Is Met
Summary
II. Into the Belly of the Functional
5. Higher-Order Functions
A Problem Report
Thunks
Chaining Functions
Fork Combinator
Alt Combinator
Compose
Transduce
Tap
Try/Catch
Handling Nulls
Update an Enumerable
Summary
6. Discriminated Unions
Holiday Time
Holidays with Discriminated Unions
Schrödinger’s Union
Naming Conventions
Database Lookup
Sending Email
Console Input
Generic Unions
Maybe
Result
Maybe Versus Result
Either
Summary
7. Functional Flow
Maybe, Revisited
Maybe and Debugging
Map() Versus Bind()
Maybe and the Primitives
Maybe and Logging
Maybe and Async
Nested Maybes
The Laws
Left Identity Law
Right Identity Law
Associativity Law
Reader
State
Maybe a State?
Examples You’re Already Using
Enumerable
Task
Other Structures
A Worked Example
Summary
8. Currying and Partial Application
Currying and Large Functions
Currying and Higher-Order Functions
Currying in .NET
Partial Application
Partial Application in .NET
Summary
9. Indefinite Loops
Recursion
Trampolining
Custom Iterator
Understanding the Anatomy of an Enumerator
Implementing Custom Enumerators
Indefinitely Looping Enumerables
Using Indefinite Iterators
Summary
10. Memoization
Bacon Numbers
Implementing Memoization in C#
Summary
III. And Out the Other Side
11. Practical Functional C#
Functional C# and Performance
Baseline: An Imperative Solution
Performance Results
Imperative baseline results
Definite loop solutions
Indefinite loop solutions
Interop with F# performance
External factors and performance
What Does All of This Mean?
Functional C# Concerns and Questions
How Functional Should I Make My Codebase?
How Should I Structure a Functional C# Solution?
How Do I Share My Functional Methods Among Applications?
Did You Order This Pizza?
How Do I Convince My Teammates to Do This Too?
Is It Worth Including F# Projects in My Solution?
Will Functional Coding Solve All My Problems?
Connery, Moore, or Craig?
How Do I Think Through a Problem Functionally?
What If There’s No Way to Make a Bit of Code as High-Performant as I’d Like with Functional-Style Code?
Summary
12. Existing Functional Programming Libraries in NuGet
OneOf
LanguageExt
Option
Either
Memoization
Reader
State
LanguageExt Wrap-up
Functional.Maybe
CSharpFunctionalExtensions
Maybe
Result
Fluent Assertions
CSharpFunctionalExtensions Wrap-up
The F# Programming Language
Summary
13. The Martian Trail
Story
Technical Detail
Creating the Game
The Solution
Communications
Want to Learn How to Play?
The Inventory Setup
The Game Loop
Creating a weather report
Choosing what to do this turn
Updating progress
Summary
14. Conclusion
What Kind of Day Has It Been?
Where Do I Go from Here?
More Functional C#
Learn F#
Pure Functional Languages
What About You?
Index

Polecaj historie