Table of Contents....5
About the Author....11
About the Technical Reviewers....12
Acknowledgments....13
Introduction....14
How Is This Book Organized?....15
Prerequisite Knowledge....16
Who Is This Book For?....17
Useful Software....17
Guidelines for Using This Book....18
Source Code....19
Conventions Used in This Book....19
Final Words....20
Part I: Getting Familiar with Functional Programming....21
Chapter 1: Functional Programming Overview....22
C# Supports Multiple Paradigms....22
Functions and Methods Are Equivalent in C#....23
Q&A Session....24
Important Characteristics of FP....24
FP Treats Functions as First-Class Citizens....25
Demonstration 1....25
Output....26
Analysis....26
FP Prefers Immutability....27
Demonstration 2....27
Output....28
Q&A Session....28
Demonstration 3....29
Output....30
Analysis....30
FP Prefers Pure Functions....30
Demonstration 4....31
Output....31
Discussion of Side Effects....32
Functions with I/O....33
Are Side Effects Bad?....35
Q&A Session....36
FP Follows a Declarative Style....37
Demonstration 5....37
Output....38
Demonstration 6....38
Output....39
Q&A Session....39
Demonstration 7....41
Output....42
FP vs. OOP....43
FP Benefits....44
Q&A Session....44
Exercises....45
Summary....47
Solutions to Exercises....47
E1.1....47
E1.2....47
E1.3....48
E1.4....48
E1.5....48
E1.6....49
E1.7....50
E1.8....50
Chapter 2: Understanding Functions....52
Mathematical Background of Functions....52
Mathematical Functions vs. C# Functions....54
Representing Functions in C#....54
Using Static Methods....54
Demonstration 1....55
Output....56
Simplifying the Code....56
Q&A Session....57
Using Delegates and Lambdas....58
A Quick Review....58
Demonstration 2....59
Output....60
Additional Note....61
Using a Dictionary....61
Demonstration 3....61
Output....62
Q&A Session....62
Demonstration 4....63
Built-in Delegates Are Important....64
Higher-Order Function....65
Custom HOF....65
Demonstration 5....66
Output....67
Additional Note....67
Built-in HOF....67
Demonstration 6....67
Output....68
Analysis....68
First-Order Function....68
Refactoring Impure Functions....68
Program with Impurities....69
Demonstration 7....69
Output....70
Removing Impurities....70
Demonstration 8....70
Output....71
Additional Note....71
Q&A Session....71
Exercises....72
Summary....73
Solutions to Exercises....74
E2.1....74
E2.2....74
E2.3....75
E2.4....76
E2.5....77
E2.6....77
Chapter 3: Understanding Immutability....78
What Is Immutability?....78
Immutable Objects in .NET....78
Q&A Session....79
Reviewing Mutable Types....80
Programming with a Mutable Type....80
Demonstration 1....80
Output....81
Analysis....81
The Path Toward Immutability....81
Achieving External Immutability....81
Demonstration 2....82
Output....83
Enforcing Internal Immutability....84
Demonstration 3....86
Output....87
Better Code Using Modern Features....87
Demonstration 4....89
Output....90
Q&A Session....90
More on Immutability....91
Understanding Shallow Immutability....92
Demonstration 5....94
Output....95
Analysis....95
Searching for a Solution....96
Demonstration 6....97
Output....98
Analysis....98
Making a Better Solution....99
Demonstration 7....99
Output....101
Analysis....102
Implementing Popsicle Immutability....102
Demonstration 8....103
Output....104
Analysis....105
Q&A Session....105
Exercises....106
Summary....107
Solutions to Exercises....108
E3.1....108
E3.2....108
E3.3....108
E3.4....108
E3.5....109
Part II: Harnessing the Power of Functional Programming....111
Chapter 4: Composing Functions Using Pipelining....112
Overview....112
Coding Functional Composition....112
Q&A Session....114
Importance of Chaining Functions....116
Program Without Chaining Functions....116
Demonstration 1....117
Output....119
Analysis....119
Refactoring Using Chaining Functions....119
Demonstration 2....121
Output....122
Applying Composition....123
Using Pipelining....123
Demonstration 3....123
Output....125
Analysis....125
Q&A Session....126
Using HOFs....126
Demonstration 4....127
Output....128
Analysis....128
Q&A Session....129
Exercises....129
Summary....130
Solutions to Exercises....130
E4.1....130
E4.2....130
E4.3....131
E4.4....132
E4.5....133
Chapter 5: Composing Functions Using Currying....136
Overview of Currying....136
Program Without Currying....137
Demonstration 1....137
Output....137
Analysis....138
Using the Concept of Currying....138
Demonstration 2....139
Output....141
Analysis....141
Q&A Session....142
Demonstration 3....144
Output....145
Using External NuGet Packages....146
Using Curryfy....147
Q&A Session....147
Case Study....148
Demonstration 4....149
Output....151
Analysis....152
Q&A Session....152
Exercises....153
Summary....155
Solutions to Exercises....156
E5.1....156
E5.2....156
E5.3....157
E5.4....159
E5.5....160
E5.6....160
Chapter 6: Handling Temporal Coupling....161
Temporal Coupling Overview....162
How Does This Happen?....162
Q&A Session....162
Recognizing the Problem....162
A Program That Suffers from Temporal Coupling....163
Demonstration 1....164
Output....168
How Does It Cause Problems?....168
Q&A Session....170
Removing the Effect....171
A Better Program....172
Demonstration 2....172
Output....175
Analysis....175
Conclusion....176
Q&A Session....177
Exercises....181
Summary....183
Solutions to Exercises....183
E6.1....183
E6.2....184
E6.3....184
E6.4....184
E6.5....186
Chapter 7: Functional Patterns....187
Map Pattern....188
Understanding the Problem....189
Initial Solution....189
Demonstration 1....191
Output....192
Better Solution....193
Concise Solution....194
Q&A Session....194
Select As Map....198
Demonstration 2....200
Q&A Session....201
Introducing Functors....202
Conclusion....202
Q&A Session....203
Bind Pattern....203
Understanding the Problem....203
Initial Solution....204
FP-Based Solution....205
Demonstration 3....206
Output....207
SelectMany As Bind....207
Demonstration 4....207
What About Monads?....208
Conclusion....209
Q&A Session....209
Filter Pattern....210
Understanding the Problem....210
Initial Solution....211
Demonstration 5....212
Output....213
Where As Filter....214
Demonstration 6....214
Conclusion....215
Fold Pattern....215
Understanding the Problem....218
Solutions Using Built-in Functions....218
Demonstration 7....219
Output....220
Conclusion....221
Q&A Session....221
Revisiting ForEach....223
Exercises....224
Summary....226
Solutions to Exercises....226
E7.1....227
E7.2....227
E7.3....227
E7.4....228
E7.5....228
E7.6....228
E7.7....228
E7.8....229
Chapter 8: Exception Handling....230
Reviewing Exception Handling in OOP....230
Imperative Style of Programming....232
Demonstration 1....232
Output....233
Exception Handling in FP....233
Using language-ext....234
Introducing the Either Type....234
How Does It Work?....235
Handling a Single Exception....239
Demonstration 2....239
Output....240
Q&A Session....240
Handling Multiple Exceptions....241
Demonstration 3....241
Output....243
Analysis....243
Q&A Session....244
Chaining Exceptions....245
Demonstration 4....245
Output....249
Demonstration 5....250
Output....251
Q&A Session....252
Handling Null Values....253
Introducing the Option Type....254
How Does It Work?....255
Q&A Session....260
Partial Functions and Options....261
Q&A Session....263
Exercises....263
Summary....266
Solutions to Exercises....267
E8.1....267
E8.2....267
E8.3....267
E8.4....268
Output....268
E8.5....269
Output....270
Chapter 9: Miscellaneous Topics....271
Helpful Features for FP....271
Delegates and Lambdas....271
Anonymous Methods....272
Extension Methods and LINQ....273
Q&A Session....275
Type Inference....277
Importing Static Members....277
Q&A Session....278
Immutability Features....278
Expression-Bodied Members....279
Q&A Session....282
Local Functions....282
Q&A Session....283
Tuples....284
Q&A Session....285
Pattern Matching....286
Revisiting Option....289
Bind Function....291
Return Function....293
Introducing Monads....294
Definition....294
Mathematical Background....295
Monad Laws....295
Understanding the Haskell Notations....296
Monad Laws for C# Developers....296
Chaining Multi-argument Functions....297
Final Suggestions....299
Command Query Separation....299
Learn Design Patterns and Anti-Patterns....299
Don’t Let Failures Stop You....300
How Much Functional Code Do I Need?....301
Applications of FP....301
The Road Ahead....301
Exercises....302
Summary....304
Solutions to Exercises....305
E9.1....305
E9.2....305
E9.3....305
E9.4....306
E9.5....306
E9.6....306
E9.7....306
Appendix: Recommended Resources....308
Books....308
Documentation....308
Courses....309
Other Resources....309
Index....310
Understand the power of C#, a multi-paradigm language. This book will help you learn, analyze, and harness the functional aspects of C# so you can create a better program.
The book is divided into two parts. Part I starts with an overview of functional programming (FP). It covers the building blocks for FP: functions and immutability. You will learn about the built-in supports for FP, first- and higher-order functions (HOF), functional purity and side effects, different forms of immutability, and much more. Part II includes discussions on functional composition using pipelining and currying. It also explains how to handle temporal coupling and discusses partial functions and useful functional patterns (such as Map, Bind, Filter, and Fold). Author Vaskaran Sarcar takes you through functional exception-handling mechanisms and provides additional tips to make your programs more efficient.
After reading the book, you will understand and be able to implement key aspects of FP. You will be able to produce better code (and refactor existing code) by combining the best techniques from both FP and object-oriented programming (OOP) paradigms.
C# developers familiar with object-oriented programming (OOP) who want to harness the power of functional programming (FP).