**Summary** __Functional Programming in C#__ teaches you to apply functional thinking to real-world problems using the C# language. The book, with its many practical examples, is written for proficient C# programmers with no prior FP experience. It will give you an awesome new perspective. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. **About the Technology** Functional programming changes the way you think about code. For C# developers, FP techniques can greatly improve state management, concurrency, event handling, and long-term code maintenance. And C# offers the flexibility that allows you to benefit fully from the application of functional techniques. This book gives you the awesome power of a new perspective. **About the Book** __Functional Programming in C#__ teaches you to apply functional thinking to real-world problems using the C# language. You'll start by learning the principles of functional programming and the language features that allow you to program functionally. As you explore the many practical examples, you'll learn the power of function composition, data flow programming, immutable data structures, and monadic composition with LINQ. **What's Inside** * Write readable, team-friendly code * Master async and data streams * Radically improve error handling * Event sourcing and other FP patterns **About the Reader** Written for proficient C# programmers with no prior FP experience. **About the Author** **Enrico Buonanno** studied computer science at Columbia University and has 15 years of experience as a developer, architect, and trainer. **Table of Contents** PART 1 - CORE CONCEPTS2. Introducing functional programming 3. Why function purity matters 4. Designing function signatures and types 5. Patterns in functional programming 6. Designing programs with function composition PART 2 - BECOMING FUNCTIONAL8. Functional error handling 9. Structuring an application with functions 10. Working effectively with multi-argument functions 11. Thinking about data functionally 12. Event sourcing: a functional approach to persistence PART 3 - ADVANCED TECHNIQUES14. Lazy computations, continuations, and the beauty of monadic composition 15. Stateful programs and stateful computations 16. Working with asynchronous computations 17. Data streams and the Reactive Extensions 18. An introduction to message-passing concurrency Dedication 4 About this Book 22 Who should read this book 23 How this book is organized 24 Code conventions and downloads 26 Book forum 27 About the author 28 Part 1. Core concepts 29 Chapter 1. Introducing functional programming 30 1.1. What is this thing called functional programming? 31 1.2. How functional a language is C#? 37 1.3. Thinking in functions 42 1.4. Higher-order functions 48 1.5. Using HOFs to avoid duplication 52 1.6. Benefits of functional programming 59 Chapter 2. Why function purity matters 61 2.1. What is function purity? 62 2.2. Purity and concurrency 66 2.3. Purity and testability 72 2.4. Purity and the evolution of computing 81 Chapter 3. Designing function signatures and types 83 3.1. Function signature design 84 3.2. Capturing data with data objects 87 3.3. Modeling the absence of data with Unit 94 3.4. Modeling the possible absence of data with Option 98 Chapter 4. Patterns in functional programming 113 4.1. Applying a function to a structure’s inner values 114 4.2. Performing side effects with ForEach 121 4.3. Chaining functions with Bind 123 4.4. Filtering values with Where 128 4.5. Combining Option and IEnumerable with Bind 130 4.6. Coding at different levels of abstraction 132 Chapter 5. Designing programs with function composition 138 5.1. Function composition 139 5.2. Thinking in terms of data flow 142 5.3. Programming workflows 146 5.4. An introduction to functional domain modeling 150 5.5. An end-to-end server-side workflow 152 Part 2. Becoming functional 158 Chapter 6. Functional error handling 159 6.1. A safer way to represent outcomes 160 6.2. Chaining operations that may fail 167 6.3. Validation: a perfect use case for Either 169 6.4. Representing outcomes to client applications 173 6.5. Variations on the Either theme 178 Chapter 7. Structuring an application with functions 186 7.1. Partial application: supplying arguments piecemeal 187 7.2. Overcoming the quirks of method resolution 192 7.3. Curried functions: optimized for partial application 195 7.4. Creating a partial-application-friendly API 197 7.5. Modularizing and composing an application 202 7.6. Reducing a list to a single value 209 Chapter 8. Working effectively with multi-argument functions 214 8.1. Function application in the elevated world 215 8.2. Functors, applicatives, monads 223 8.3. The monad laws 225 8.4. Improving readability by using LINQ with any monad 229 8.5. When to use Bind vs. Apply 235 Chapter 9. Thinking about data functionally 240 9.1. The pitfalls of state mutation 241 9.2. Understanding state, identity, and change 244 9.3. Enforcing immutability 250 9.4. A short introduction to functional data structures 257 Chapter 10. Event sourcing: a functional approach to persistence 268 10.1. Thinking functionally about data storage 269 10.2. Event sourcing basics 272 10.3. Architecture of an event-sourced system 281 10.4. Comparing different approaches to immutable storage 291 Part 3. Advanced techniques 294 Chapter 11. Lazy computations, continuations, and the beauty of monadic composition 295 11.1. The virtue of laziness 296 11.2. Exception handling with Try 301 11.3. Creating a middleware pipeline for DB access 306 Summary 316 Chapter 12. Stateful programs and stateful computations 317 12.1. Programs that manage state 318 12.2. A language for generating random data 323 12.3. A general pattern for stateful computations 328 Summary 331 Chapter 13. Working with asynchronous computations 332 13.1. Asynchronous computations 333 13.2. Traversables: working with lists of elevated values 343 13.3. Combining asynchrony and validation (or any other two monadic effects) 352 Summary 356 Chapter 14. Data streams and the Reactive Extensions 357 14.1. Representing data streams with IObservable 358 14.2. Creating IObservables 362 14.3. Transforming and combining data streams 366 14.4. Implementing logic that spans multiple events 373 14.5. When should you use IObservable? 380 Summary 381 Chapter 15. An introduction to message-passing concurrency 382 15.1. The need for shared mutable state 383 15.2. Understanding message-passing concurrency 385 15.3. Functional APIs, agent-based implementations 395 15.4. Message-passing concurrency in LOB applications 398 Summary 406 Inverted chapter dependency graph 409 Functional programming is a way of thinking about programs that emphasizes functions, while avoiding state mutation. It allows us to write elegant, intention-revealing code, that shines in testability and support for concurrency. C# includes a number of functional features and libraries, enabling us to take advantage of these benefits. Functional Programming in C# teaches you to apply functional thinking to real-world scenarios. Youll start by learning the principles of functional programming, and how they translate in the C# language. The book then dives into important topics like function composition, data flow, and principles for designing function signatures, types and collections. Through lots of real-world examples, youll acquire the tools to tackle programming tasks with a functional approach. The last part of the book deals with advanced topics, including lazy evaluation, stateful computations, asynchrony, and event streams. By the end of this book, youll be able to integrate functional techniques, making your C# programs robust and maintainable, and helping you become a more well-rounded developer.