Functional Programming with C#

Book description

After decades of relative obscurity, functional programming is finally coming into its own. With concise, easy-to-read code that supports asynchronous, concurrent processing, aspects of functional programming have begun to appear in several traditionally object-oriented languages such as C# and Java. This practical book shows C# programmers how to use functional programming features without having to navigate an entirely new language.

Because of the shared runtime environment common to C# and F# languages, it's possible to use most of F#'s functional features in C# as well. Author Simon J. Painter explains how you can write functional code in C# right away, without having to install dependencies or features newer than .NET 3. You'll learn why functional programming concepts can bring immediate benefit to your work.

  • Learn what functional programming is and how it originated
  • Discover features of the functional paradigm using a more familiar language
  • Start coding functionally in C# right away, without relying on third-party libraries
  • Write code that's more robust, less error prone, and easier to test
  • Examine less conventional ways to look at structures available in C#
  • Explore the practicalities of using functional C# in a business environment

Table of contents

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

Product information

  • Title: Functional Programming with C#
  • Author(s): Simon J. Painter
  • Release date: September 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492097075