#Functional Programming with C

by Alex Yagur

Programming

Book Details

Book Title

#Functional Programming with C

Author

Alex Yagur

Publisher

Packt Publishing Pvt Ltd

Publication Date

2024

ISBN

9781805122685

Number of Pages

239

Language

English

Format

PDF

File Size

3.6MB

Subject

Csharp

Table of Contents

  • Contributors
  • About the author
  • About the reviewers
  • Preface
  • Part 1: Foundations of Functional Programming in C#
  • Chapter 1: Getting Started with Functional Programming
  • Functional versus imperative versus object-oriented programming
  • How functional programming is supported in C#
  • How to write functional code in C#
  • A practical example – a book publishing system
  • How to combine functional and object-oriented paradigms
  • Meet Steve and Julia
  • Summary
  • Chapter 2: Expressions and Statements
  • Task 1 – Name and count all expressions and all statements
  • Task 2 – Use expressions instead of statements
  • Task 3 – Create an expression tree
  • Understanding the difference between expressions and statements
  • How to use expressions for clear and simple code
  • Lambda expressions, expression-bodied members, and anonymous methods
  • Expression trees and how to use them to manipulate expressions at runtime
  • Guided exercise – constructing and manipulating expression trees
  • Problem sets and exercises
  • Exercises
  • Solutions
  • Summary
  • Chapter 3: Pure Functions and Side Effects
  • Task 1 – Refactoring to a pure function
  • Task 2 – Isolating side effects
  • Task 3 – Using a Pure attribute
  • Understanding pure functions
  • Side effects
  • Strategies to minimize side effects
  • Marking pure functions with the Pure attribute
  • Exercises
  • Solutions
  • Summary
  • Chapter 4: Honest Functions, Null, and Option
  • Task 1 – Refactor for honest return types
  • Task 2 – Guard against null inputs
  • Task 3 – Pattern matching with nullable types
  • Honest functions – definition and understanding
  • The problems with hidden nulls
  • Embracing honesty with nullable reference types
  • Returning with intention
  • Demanding honesty from function inputs
  • Pattern matching and nullable types
  • The null object pattern
  • Beyond null – using Option
  • Practical scenarios – handling nulls effectively
  • The reality of honesty in C# – why there will never be truly honest functions
  • The compromises of the C# language design
  • Practical tips and best practices
  • Exercises
  • Solutions
  • Summary
  • Part 2: Advanced Functional Techniques
  • Chapter 5: Error Handling
  • Task 1 – Custom error types and result usage
  • Task 2 – Utilizing ROP for validation and processing
  • Task 3 – Implementing a Retry mechanism using functional techniques
  • Traditional error handling in C#
  • The Result type
  • Railway-Oriented Programming (ROP)
  • Designing your own error-handling mechanisms
  • Practical tips for functional error handling
  • Traditional versus functional error handling comparison
  • Patterns and anti-patterns in functional error handling
  • Solutions
  • Summary
  • Chapter 6: Higher-Order Functions and Delegates
  • Task 1 – Sorting function
  • Task 2 – Customized calculations
  • Task 3 – Comparison
  • Understanding higher-order functions
  • Delegates, actions, funcs, and predicates
  • Callbacks, events, and anonymous methods
  • Harnessing LINQ methods as higher-order functions
  • Case study – putting it all together
  • Best practices and common pitfalls
  • Exercises
  • Solutions
  • Summary
  • Chapter 7: Functors and Monads
  • Task 1 – Functor usage
  • Task 2 – Applicative functor
  • Task 3 – Monad usage
  • What’s a functor?
  • Applicative functors
  • Monads
  • Key takeaways
  • Exercises
  • Solutions
  • Summary
  • Part 3: Practical Functional Programming
  • Chapter 8: Recursion and Tail Calls
  • Task 1 – Recursive enemy count
  • Task 2 – Wave generation
  • Task 3 – Asynchronously updating enemy stats
  • Introducing recursion
  • Recursive thinking
  • Types of recursion
  • Challenges of recursion
  • Leveraging C# features for recursion
  • Advanced recursive patterns
  • Comparison with iterative solutions
  • Recursion in asynchronous programming
  • Synchronous versus asynchronous recursion
  • Exercises
  • Solutions
  • Summary
  • Chapter 9: Currying and Partial Application
  • Task 1 – Currying tower attack functions
  • Task 2 – Partial application for game settings
  • Task 3 – Currying permission checks for game features
  • Understanding currying
  • Step-by-step implementation of currying
  • Partial application
  • Areas for partial application
  • Challenges and limitations
  • Exercises
  • Solutions
  • Summary
  • Chapter 10: Pipelines and Composition
  • Task 1 – Enemy wave processing pipeline
  • Task 2 – Game data file processing
  • Task 3 – Dynamic SQL query generation using currying and partial application
  • Function composition
  • Building pipelines
  • Performance considerations
  • The fluent interface
  • Advanced composition with monads
  • Exercises
  • Solutions
  • Summary
  • Part 4: Conclusion and Future Directions
  • Chapter 11: Reflecting and Looking Ahead
  • A summary of the main concepts and techniques
  • Comparison with other languages
  • Resources for further learning
  • Closing thoughts
  • Index
  • Other Books You May Enjoy