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 blo
Introducing Functional Programming Using C#: Leveraging a New Perspective for OOP Developers
✍ Scribed by Vaskaran Sarcar
- Publisher
- Apress
- Year
- 2023
- Tongue
- English
- Leaves
- 314
- Edition
- 1
- Category
- Library
No coin nor oath required. For personal study only.
✦ Synopsis
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.
What Will You Learn
- Understand how the concepts of functional programming (FP) can be implemented using C#
- Refactor existing code (following the FP paradigm) to make it more readable, maintainable, and predictive
- Know how advanced features (such as delegates, lambdas, generics) can be used differently
- Understand how to efficiently use features such as lambda expressions and type inference as C# includes explicit language extensions to support functional programming
Who This Book Is For
C# developers familiar with object-oriented programming (OOP) who want to harness the power of functional programming (FP)
✦ Table of Contents
Table of Contents
About the Author
About the Technical Reviewers
Acknowledgments
Introduction
How Is This Book Organized?
Prerequisite Knowledge
Who Is This Book For?
Useful Software
Guidelines for Using This Book
Source Code
Conventions Used in This Book
Final Words
Part I: Getting Familiar with Functional Programming
Chapter 1: Functional Programming Overview
C# Supports Multiple Paradigms
Functions and Methods Are Equivalent in C#
Q&A Session
Important Characteristics of FP
FP Treats Functions as First-Class Citizens
Demonstration 1
Output
Analysis
FP Prefers Immutability
Demonstration 2
Output
Q&A Session
Demonstration 3
Output
Analysis
FP Prefers Pure Functions
Demonstration 4
Output
Discussion of Side Effects
Functions with I/O
Are Side Effects Bad?
Q&A Session
FP Follows a Declarative Style
Demonstration 5
Output
Demonstration 6
Output
Q&A Session
Demonstration 7
Output
FP vs. OOP
FP Benefits
Q&A Session
Exercises
Summary
Solutions to Exercises
E1.1
E1.2
E1.3
E1.4
E1.5
E1.6
E1.7
E1.8
Chapter 2: Understanding Functions
Mathematical Background of Functions
Mathematical Functions vs. C# Functions
Representing Functions in C#
Using Static Methods
Demonstration 1
Output
Simplifying the Code
Q&A Session
Using Delegates and Lambdas
A Quick Review
Demonstration 2
Output
Additional Note
Using a Dictionary
Demonstration 3
Output
Q&A Session
Demonstration 4
Built-in Delegates Are Important
Higher-Order Function
Custom HOF
Demonstration 5
Output
Additional Note
Built-in HOF
Demonstration 6
Output
Analysis
First-Order Function
Refactoring Impure Functions
Program with Impurities
Demonstration 7
Output
Removing Impurities
Demonstration 8
Output
Additional Note
Q&A Session
Exercises
Summary
Solutions to Exercises
E2.1
E2.2
E2.3
E2.4
E2.5
E2.6
Chapter 3: Understanding Immutability
What Is Immutability?
Immutable Objects in .NET
Q&A Session
Reviewing Mutable Types
Programming with a Mutable Type
Demonstration 1
Output
Analysis
The Path Toward Immutability
Achieving External Immutability
Demonstration 2
Output
Enforcing Internal Immutability
Demonstration 3
Output
Better Code Using Modern Features
Demonstration 4
Output
Q&A Session
More on Immutability
Understanding Shallow Immutability
Demonstration 5
Output
Analysis
Searching for a Solution
Demonstration 6
Output
Analysis
Making a Better Solution
Demonstration 7
Output
Analysis
Implementing Popsicle Immutability
Demonstration 8
Output
Analysis
Q&A Session
Exercises
Summary
Solutions to Exercises
E3.1
E3.2
E3.3
E3.4
E3.5
Part II: Harnessing the Power of Functional Programming
Chapter 4: Composing Functions Using Pipelining
Overview
Coding Functional Composition
Q&A Session
Importance of Chaining Functions
Program Without Chaining Functions
Demonstration 1
Output
Analysis
Refactoring Using Chaining Functions
Demonstration 2
Output
Applying Composition
Using Pipelining
Demonstration 3
Output
Analysis
Q&A Session
Using HOFs
Demonstration 4
Output
Analysis
Q&A Session
Exercises
Summary
Solutions to Exercises
E4.1
E4.2
E4.3
E4.4
E4.5
Chapter 5: Composing Functions Using Currying
Overview of Currying
Program Without Currying
Demonstration 1
Output
Analysis
Using the Concept of Currying
Demonstration 2
Output
Analysis
Q&A Session
Demonstration 3
Output
Using External NuGet Packages
Using Curryfy
Q&A Session
Case Study
Demonstration 4
Output
Analysis
Q&A Session
Exercises
Summary
Solutions to Exercises
E5.1
E5.2
E5.3
E5.4
E5.5
E5.6
Chapter 6: Handling Temporal Coupling
Temporal Coupling Overview
How Does This Happen?
Q&A Session
Recognizing the Problem
A Program That Suffers from Temporal Coupling
Demonstration 1
Output
How Does It Cause Problems?
Q&A Session
Removing the Effect
A Better Program
Demonstration 2
Output
Analysis
Conclusion
Q&A Session
Exercises
Summary
Solutions to Exercises
E6.1
E6.2
E6.3
E6.4
E6.5
Chapter 7: Functional Patterns
Map Pattern
Understanding the Problem
Initial Solution
Demonstration 1
Output
Better Solution
Concise Solution
Q&A Session
Select As Map
Demonstration 2
Q&A Session
Introducing Functors
Conclusion
Q&A Session
Bind Pattern
Understanding the Problem
Initial Solution
FP-Based Solution
Demonstration 3
Output
SelectMany As Bind
Demonstration 4
What About Monads?
Conclusion
Q&A Session
Filter Pattern
Understanding the Problem
Initial Solution
Demonstration 5
Output
Where As Filter
Demonstration 6
Conclusion
Fold Pattern
Understanding the Problem
Solutions Using Built-in Functions
Demonstration 7
Output
Conclusion
Q&A Session
Revisiting ForEach
Exercises
Summary
Solutions to Exercises
E7.1
E7.2
E7.3
E7.4
E7.5
E7.6
E7.7
E7.8
Chapter 8: Exception Handling
Reviewing Exception Handling in OOP
Imperative Style of Programming
Demonstration 1
Output
Exception Handling in FP
Using language-ext
Introducing the Either Type
How Does It Work?
Handling a Single Exception
Demonstration 2
Output
Q&A Session
Handling Multiple Exceptions
Demonstration 3
Output
Analysis
Q&A Session
Chaining Exceptions
Demonstration 4
Output
Demonstration 5
Output
Q&A Session
Handling Null Values
Introducing the Option Type
How Does It Work?
Q&A Session
Partial Functions and Options
Q&A Session
Exercises
Summary
Solutions to Exercises
E8.1
E8.2
E8.3
E8.4
Output
E8.5
Output
Chapter 9: Miscellaneous Topics
Helpful Features for FP
Delegates and Lambdas
Anonymous Methods
Extension Methods and LINQ
Q&A Session
Type Inference
Importing Static Members
Q&A Session
Immutability Features
Expression-Bodied Members
Q&A Session
Local Functions
Q&A Session
Tuples
Q&A Session
Pattern Matching
Revisiting Option
Bind Function
Return Function
Introducing Monads
Definition
Mathematical Background
Monad Laws
Understanding the Haskell Notations
Monad Laws for C# Developers
Chaining Multi-argument Functions
Final Suggestions
Command Query Separation
Learn Design Patterns and Anti-Patterns
Don’t Let Failures Stop You
How Much Functional Code Do I Need?
Applications of FP
The Road Ahead
Exercises
Summary
Solutions to Exercises
E9.1
E9.2
E9.3
E9.4
E9.5
E9.6
E9.7
Appendix: Recommended Resources
Books
Documentation
Courses
Other Resources
Index
📜 SIMILAR VOLUMES
Software development today is embracing functional programming (FP), whether it's for writing concurrent programs or for managing Big Data. Where does that leave Java developers? This concise book offers a pragmatic, approachable introduction to FP for Java developers or anyone who uses an object-or
<br>Software development today is embracing functional programming (FP), whether its for writing concurrent programs or for managing Big Data. Where does that leave Java developers? This concise book offers a pragmatic, approachable introduction to FP for Java developers or anyone who uses an object
<p>Software development today is embracing functional programming (FP), whether it’s to write concurrent programs or to manage Big Data. Where does that leave Java developers? This concise book offers a pragmatic, approachable introduction to FP for Java developers or anyone who uses an object