<p>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 J
Functional Programming with C#: Create More Supportable, Robust, and Testable Code
β Scribed by Simon Painter
- Publisher
- O'Reilly Media
- Year
- 2023
- Tongue
- English
- Leaves
- 328
- Edition
- 1
- Category
- Library
No coin nor oath required. For personal study only.
β¦ Synopsis
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
Copyright
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
Chapter 1. Introduction
What Is Functional Programming?
Is It a Language, an API, or What?
The Properties of Functional Programming
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
Part I. What Are We Already Doing?
Chapter 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
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
Chapter 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
Chapter 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
Part II. Into the Belly of the Functional
Chapter 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
Chapter 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
Chapter 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
Chapter 8. Currying and Partial Application
Currying and Large Functions
Currying and Higher-Order Functions
Currying in .NET
Partial Application
Partial Application in .NET
Summary
Chapter 9. Indefinite Loops
Recursion
Trampolining
Custom Iterator
Understanding the Anatomy of an Enumerator
Implementing Custom Enumerators
Indefinitely Looping Enumerables
Using Indefinite Iterators
Summary
Chapter 10. Memoization
Bacon Numbers
Implementing Memoization in C#
Summary
Part III. And Out the Other Side
Chapter 11. Practical Functional C#
Functional C# and Performance
Baseline: An Imperative Solution
Performance Results
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
Chapter 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
Chapter 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
Summary
Chapter 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
About the Author
Colophon
β¦ Subjects
C#; Functional Programming; Currying; Memoization; Higher-Order Functions
π SIMILAR VOLUMES
<p><span>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#
<p><span>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#
Explore functional and reactive programming to create robust and testable TypeScript applications
<p><span>A comprehensive guide to understanding network architecture, communication protocols, and network analysis to build secure applications compatible with the latest versions of C# 8 and .NET Core 3.0</span></p><h4><span>Key Features</span></h4><ul><li><span><span>Explore various network archi
Code <p><b>Build safety-critical and memory-safe stand-alone and networked embedded systems</b></p> <h4>Key Features</h4> <ul><li>Know how C++ works and compares to other languages used for embedded development </li> <li>Create advanced GUIs for embedded devices to design an attractive and function