Functional Programming in C#, Second Edition
Real world examples and practical techniques for functional programming in C# without the jargon and theory.

In Functional Programming in C#, Second Edition you will learn how to:

Use higher-order functions to reduce duplication and do more with less code
Use pure functions to write code that is easy to test and optimize
Write pleasant APIs that accurately describe your program's behavior
Use dedicated types to handle nullability, system errors, and validation rules predictably and elegantly
Write composable code without the overhead of an IoC container

Functional Programming in C# has helped thousands of developers apply functional thinking to C# code. Its practical examples and spot-on treatment of FP concepts makes it the perfect guide for proficient C# programmers. This second edition is fully revised to cover new functional-inspired features in the most recent releases of C#, including tuples, async streams, pattern matching, and records. Each chapter is packed with awesome perspectives and epiphany moments on how functional programming can change the way you code.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the technology
Turbocharge your C# code. Good functional techniques will improve concurrency, state management, event handling, and maintainability of your software. This book gives you practical answers to why, how, and where to add functional programing into your C# coding practice.

About the book
Functional Programming in C#, Second Edition teaches functional thinking for real-world problems. It reviews the C# language features that allow you to program functionally and through many practical examples shows the power of function composition, data-driven programming, and immutable data structures. All code examples work with .NET 6 and C# 10.

What's inside

Higher-order functions reduce duplication and do more with less code
Code based on pure functions is easy to test and optimize
Write pleasant APIs that accurately describe your program’s behavior
Write a Web API in a functional style
Monadic composition with LINQ

About the reader
For intermediate C# programmers.

About the author
Enrico Buonanno studied Computer Science at Columbia University and has over 15 years of experience as a developer, architect, and trainer.

Table of Contents
PART 1 GETTING STARTED
1 Introducing functional programming
2 Thinking in functions
3 Why function purity matters
PART 2 CORE TECHNIQUES
4 Designing function signatures and types
5 Modeling the possible absence of data
6 Patterns in functional programming
7 Designing programs with function composition
PART 3 FUNCTIONAL DESIGNS
8 Functional error handling
9 Structuring an application with functions
10 Working effectively with multi-argument functions
11 Representing state and change
12 A short introduction to functional data structures
13 Event sourcing: A functional approach to persistence
PART 4 ADVANCED TECHNIQUES
14 Lazy computations, continuations, and the beauty of monadic composition
15 Stateful programs and stateful computations
16 Working with asynchronous computations
17 Traversable and stacked monads
18 Data streams and the Reactive Extensions
19 An introduction to message-passing concurrency
1139545320
Functional Programming in C#, Second Edition
Real world examples and practical techniques for functional programming in C# without the jargon and theory.

In Functional Programming in C#, Second Edition you will learn how to:

Use higher-order functions to reduce duplication and do more with less code
Use pure functions to write code that is easy to test and optimize
Write pleasant APIs that accurately describe your program's behavior
Use dedicated types to handle nullability, system errors, and validation rules predictably and elegantly
Write composable code without the overhead of an IoC container

Functional Programming in C# has helped thousands of developers apply functional thinking to C# code. Its practical examples and spot-on treatment of FP concepts makes it the perfect guide for proficient C# programmers. This second edition is fully revised to cover new functional-inspired features in the most recent releases of C#, including tuples, async streams, pattern matching, and records. Each chapter is packed with awesome perspectives and epiphany moments on how functional programming can change the way you code.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the technology
Turbocharge your C# code. Good functional techniques will improve concurrency, state management, event handling, and maintainability of your software. This book gives you practical answers to why, how, and where to add functional programing into your C# coding practice.

About the book
Functional Programming in C#, Second Edition teaches functional thinking for real-world problems. It reviews the C# language features that allow you to program functionally and through many practical examples shows the power of function composition, data-driven programming, and immutable data structures. All code examples work with .NET 6 and C# 10.

What's inside

Higher-order functions reduce duplication and do more with less code
Code based on pure functions is easy to test and optimize
Write pleasant APIs that accurately describe your program’s behavior
Write a Web API in a functional style
Monadic composition with LINQ

About the reader
For intermediate C# programmers.

About the author
Enrico Buonanno studied Computer Science at Columbia University and has over 15 years of experience as a developer, architect, and trainer.

Table of Contents
PART 1 GETTING STARTED
1 Introducing functional programming
2 Thinking in functions
3 Why function purity matters
PART 2 CORE TECHNIQUES
4 Designing function signatures and types
5 Modeling the possible absence of data
6 Patterns in functional programming
7 Designing programs with function composition
PART 3 FUNCTIONAL DESIGNS
8 Functional error handling
9 Structuring an application with functions
10 Working effectively with multi-argument functions
11 Representing state and change
12 A short introduction to functional data structures
13 Event sourcing: A functional approach to persistence
PART 4 ADVANCED TECHNIQUES
14 Lazy computations, continuations, and the beauty of monadic composition
15 Stateful programs and stateful computations
16 Working with asynchronous computations
17 Traversable and stacked monads
18 Data streams and the Reactive Extensions
19 An introduction to message-passing concurrency
59.99 In Stock
Functional Programming in C#, Second Edition

Functional Programming in C#, Second Edition

by Enrico Buonanno
Functional Programming in C#, Second Edition

Functional Programming in C#, Second Edition

by Enrico Buonanno

Paperback(2nd ed.)

$59.99 
  • SHIP THIS ITEM
    In stock. Ships in 2-4 days.
  • PICK UP IN STORE

    Your local store may have stock of this item.

Related collections and offers


Overview

Real world examples and practical techniques for functional programming in C# without the jargon and theory.

In Functional Programming in C#, Second Edition you will learn how to:

Use higher-order functions to reduce duplication and do more with less code
Use pure functions to write code that is easy to test and optimize
Write pleasant APIs that accurately describe your program's behavior
Use dedicated types to handle nullability, system errors, and validation rules predictably and elegantly
Write composable code without the overhead of an IoC container

Functional Programming in C# has helped thousands of developers apply functional thinking to C# code. Its practical examples and spot-on treatment of FP concepts makes it the perfect guide for proficient C# programmers. This second edition is fully revised to cover new functional-inspired features in the most recent releases of C#, including tuples, async streams, pattern matching, and records. Each chapter is packed with awesome perspectives and epiphany moments on how functional programming can change the way you code.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the technology
Turbocharge your C# code. Good functional techniques will improve concurrency, state management, event handling, and maintainability of your software. This book gives you practical answers to why, how, and where to add functional programing into your C# coding practice.

About the book
Functional Programming in C#, Second Edition teaches functional thinking for real-world problems. It reviews the C# language features that allow you to program functionally and through many practical examples shows the power of function composition, data-driven programming, and immutable data structures. All code examples work with .NET 6 and C# 10.

What's inside

Higher-order functions reduce duplication and do more with less code
Code based on pure functions is easy to test and optimize
Write pleasant APIs that accurately describe your program’s behavior
Write a Web API in a functional style
Monadic composition with LINQ

About the reader
For intermediate C# programmers.

About the author
Enrico Buonanno studied Computer Science at Columbia University and has over 15 years of experience as a developer, architect, and trainer.

Table of Contents
PART 1 GETTING STARTED
1 Introducing functional programming
2 Thinking in functions
3 Why function purity matters
PART 2 CORE TECHNIQUES
4 Designing function signatures and types
5 Modeling the possible absence of data
6 Patterns in functional programming
7 Designing programs with function composition
PART 3 FUNCTIONAL DESIGNS
8 Functional error handling
9 Structuring an application with functions
10 Working effectively with multi-argument functions
11 Representing state and change
12 A short introduction to functional data structures
13 Event sourcing: A functional approach to persistence
PART 4 ADVANCED TECHNIQUES
14 Lazy computations, continuations, and the beauty of monadic composition
15 Stateful programs and stateful computations
16 Working with asynchronous computations
17 Traversable and stacked monads
18 Data streams and the Reactive Extensions
19 An introduction to message-passing concurrency

Product Details

ISBN-13: 9781617299827
Publisher: Manning
Publication date: 02/01/2022
Edition description: 2nd ed.
Pages: 448
Product dimensions: 7.38(w) x 9.25(h) x 0.90(d)

About the Author

Enrico Buonanno studied computer science at Columbia University and has over 15 years of experience as a developer, architect, and trainer.

Table of Contents

Preface xv

Acknowledgments xvii

About this book xviii

About the author xxii

Part 1 Getting Started 1

1 Introducing functional programming 3

1.1 What is this thing called functional programming? 4

Functions as first-class values 4

Avoiding state mutation 5

Writing programs with strong guarantees 6

1.2 How functional a language is C#? 9

The functional nature of LINQ 9

Shorthand syntax for coding functionally 10

Language support for tuples 12

Pattern matching and record types 14

1.3 What you will learn in this book 18

2 Thinking in functions 19

2.1 What's a function, anyway? 19

Functions as maps 20

Representing functions in C# 20

2.2 Higher-order functions (HOFs) 25

Functions that depend on other functions 25

Adapter functions 27

Functions that create other functions 28

2.3 Using HOFs to avoid duplication 29

Exercises 32

3 Why function purity matters 33

3.1 What is function purity? 34

Purity and side effects 34

Strategies for managing side effects 35

Avoid mutating arguments 36

3.2 Enabling parallelization by avoiding state mutation 37

Pure functions parallelize well 39

Parallelizing impure functions 40

Avoiding state mutation 42

3.3 Purity and testability 44

Isolating I/O effects 44

A business validation scenario 46

Why testing impure functions is hard 48

3.4 Testing code that performs 1/O 50

Object-oriented dependency injection 50

Testability without so much boilerplate 54

3.5 Purity and the evolution of computing 57

Exercises 57

Part 2 Core Techniques 59

4 Designing function signatures and types 61

4.1 Designing function signatures 62

Writing functions signatures with arrow notation 62

How informative is a signature? 63

4.2 Capturing data with data objects 64

Primitive types are often not specific enough 65

Constraining inputs with custom types 65

Writing honest functions 67

Composing values into complex data objects 69

4.3 Modeling the absence of data with Unit 70

Why void isn't ideal 71

Bridging the gap between Action and Func 72

5 Modeling the possible absence of data 75

5.1 The bad APIs you use every day 76

5.2 An introduction to the Option type 77

5.3 Implementing Option 79

An idealized implementation of Option 79

Consuming an Option 80

Creating a None 81

Creating a Some 82

Optimizing the Option implementation 83

5.4 Option as the natural result type of partial functions 84

Parsing strings 85

Looking up data in a collection 86

The smart constructor pattern 87

5.5 Dealing with null 88

Why null is such a terrible idea 88

Gaining robustness by using Option instead of null 89

Non-nullable reference types? 90

Bulletproof against NullReferenceException 92

Exercises 94

6 Patterns in functional programming 95

6.1 Applying a function to a structure's inner values 96

Mapping a function onto a sequence 96

Mapping a function onto an Option 97

How Option raises the level of abstraction 99

Introducing functors 100

6.2 Performing side effects with ForEach 101

6.3 Chaining functions with Bind 103

Combining Option-returning functions 104

Flattening nested lists with Bind 105

Actually, it's called a monad 107

The Return function 107

Relationship between functors and monads 108

6.4 Filtering values with Where 108

6.5 Combining Option and IEnumerable with Bind 109

6.6 Coding at different levels of abstraction 111

Regular vs. elevated values 111

Crossing levels of abstraction 112

Map vs. Bind, revisited 114

Working at the right level of abstraction 114

Exercises 115

7 Designing programs with function composition 117

7.1 Function composition 118

Brushing up an function composition 118

Method chaining 119

Composition in the elevated world 120

7.2 Thinking in terms of data flow 121

Using LINQ's composable API 121

Writing functions that compose well 123

7.3 Programming workflows 125

A simple workflow for validation 125

Refactoring with data flow in mind 126

Composition leads to greater flexibility 127

7.4 An introduction to functional domain modeling 128

7.5 An end-to-end server-side workflow 130

Expressions vs. statements 131

Declarative vs. imperative 132

The functional take on layering 133

Exercises 135

Part 3 Functional Designs 137

8 Functional error handling 139

8.1 A safer way to represent outcomes 140

Capturing error details with Either 140

Core functions for working with Either 144

Comparing Option and Either 145

8.2 Chaining operations that may fail 146

8.3 Validation: A perfect use case for Either 148

Choosing a suitable representation for errors 149

Defining an Either-based API 150

Adding validation logic 150

8.4 Representing outcomes to client applications 151

Exposing an Option-like interface 153

Exposing an Either-like interface 154

Returning a result DTO 155

8.5 Variations on the Either theme 156

Changing between different error representations 156

Specialized versions of Either 157

Refactoring to Validation and Exceptional 158

Leaving exceptions behind? 162

Exercises 163

9 Structuring an application with functions 165

9.1 Partial application: Supplying arguments piecemeal 166

Manually enabling partial application 168

Generalizing partial application 169

Order of arguments matters 170

9.2 Overcoming the quirks of method resolution 171

9.3 Curried functions: Optimized for partial application 174

9.4 Creating a partial-application-friendly API 176

Types as documentation 177

Particularizing the data access function 178

9.5 Modularizing and composing an application 180

Modularity in OOP 181

Modularity in FP 183

Mapping functions to API endpoints 186

Comparing the two approaches 188

9.6 Reducing a list to a single value 189

LINQ's Aggregate method 189

Aggregating validation results 191

Harvesting validation errors 192

Exercises 193

10 Working effectively with multi-argument junctions 195

10.1 Function application in the elevated world 196

Understanding applicatives 198

Lifting functions 200

An introduction to property-based testing 201

10.2 Functors, applicatives, and monads 203

10.3 The monad laws 205

Right identity 205

Left identity 206

Associativity 207

Using Rind with multi-argument functions 208

10.4 Improving readability by using LINQ with any monad 208

Using LINQ with arbitrary functors 209

Using LINQ with arbitrary monads 210

The LINQ clauses let, where, and others 214

10.5 When to use Bind vs. Apply 215

Validation with smart, constructors 215

Harvesting errors with the applicative flow 216

Failing fast with the monadic flow 218

Exercises 219

11 Representing state and change 221

11.1 The pitfalls of state mutation 222

11.2 Understanding state, identity, and change 225

Some things never change 226

Representing change without mutation 228

11.3 Using records to capture the state of domain entities 229

Fine-grained control on record initialization 231

Immutable all the way down 233

11.4 Separating data and logic 235

12 A short introduction to functional data structures 239

12.1 The classic functional linked list 240

Common list operations 243

Modifying an immutable list 244

Destructuring any IEnumerable 245

12.2 Binary trees 246

Common tree operations 247

Structure sharing 249

12.3 In conclusion 250

Exercises 250

13 Event sourcing: Afunctional approach to persistence 252

13.1 Thinking functionally about data storage 253

Why data storage should be append-only 253

Relax and forget about storing slate 254

13.2 Event sourcing basics 255

Representing events 256

Persisting events 257

Representing state 258

Representing state transitions 258

Reconstructing the current state from past events 260

13.3 Architecture of an event-sourced system 262

Handling commands 263

Handling events 266

Adding validation 267

Creating views of the data from events 269

13.4 Comparing different approaches to immutable storage 272

Datomic vs. Event Store 273

How event-driven is your domain? 273

Part 4 Advanced Techniques 277

14 Lazy computations, continuations, and the beauty of monadic composition 279

14.1 The virtue of laziness 280

Lazy APIs for working with Option 281

Composing lazy computations 283

14.2 Exception handling with Try 285

Representing computations that may fail 286

Safely extracting information from a JSON object 287

Composing computations that may fail 288

Monadic composition: What does it mean? 289

14.3 Creating a middleware pipeline for DB access 291

Composing functions that perform setup/teardown 291

A recipe against the pyramid of doom 293

Capturing the essence of a middleware function 293

Implementing the query pa Item for middleware 295

Adding middleware that limes the operation 298

Adding middleware that manages a DB transaction 299

15 Stateful programs and stateful computations 302

15.1 Programs that manage state 303

Caching data in memory 304

Refactoring for testability and error handling 306

Stateful computations 308

15.2 A language for generating random data 309

Generating random integers 310

Generating other primitives 310

Generating complex structures 312

15.3 A general pattern for stateful computations 314

16 Working with asynchronous computations 318

16.1 Asynchronous computations 319

The need for asynchrony 319

Representing asynchronous operations with Task 320

Task as a container for a future value 322

Handling failure 324

An HTTP API for currency conversion 326

If it fails, try a few more times 327

Running asynchronous operations in parallel 328

16.2 Async streams 330

Reading from a file as an async stream 331

Consuming async streams functionally 333

Consuming data from several streams 333

Aggregation and sorting with async streams 334

17 Traversable and stacked monads 336

17.1 Traversables: Working with lists of elevated values 336

Validating a list of values with monadic Traverse 338

Harvesting validation errors with applicative Traverse 339

Applying multiple validators to a single value 341

Using Traverse with Task to await multiple results 342

Defining Traverse for single-value, structures 344

17.2 Combining asynchrony and validation (or any other two monadic effects) 345

The problem of stacked monads 345

Reducing the number of effects 347

LINQ expressions with a monad stack 348

18 Data streams and the Reactive Extensions 351

18.1 Representing data streams with IObservable 352

A sequence of values in time 353

Subscribing to an IObservable 354

18.2 Creadng IObservables 355

Creating a timer 356

Using Subject to tell an IObservable when it should signal 357

Creating IObservables from callback-based subscriptions 358

Creating IObservables from simpler structures 359

18.3 Transforming and combining data streams 360

Stream, transformations 360

Combining and partitioning streams 362

Error handling with IObservable 364

Putting it all together 366

18.4 Implementing logic that spans multiple events 366

Detecting sequences of pressed keys 367

Reacting to multiple event sources 369

Notifying when an account becomes overdrawn 371

18.5 When should you use IObservable? 374

19 An introduction to message-passing concurrency 376

19.1 The need for shared mutable state 377

19.2 Understanding message-passing concurrency 378

Implementing agents in C# 381

Getting started with agents 382

Using agents to handle concurrent requests 384

Agents vs. actors 387

19.3 Functional APIs, agent-based implementations 389

Agents as implementation details 390

Hiding agents behind a conventional API 391

19.4 Message-passing concurrency in LOB applications 392

Using an agent to synchronize access to account data 393

Keeping a registry of accounts 394

An agent is not an object 395

Putting it all together 398

Appendix A Working with previous version of C# 401

Epilogue What next? 413

Index 415

From the B&N Reads Blog

Customer Reviews