
Functional Programming with C#
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
In an era where software complexity is increasing by the day, functional programming offers a paradigm that simplifies code maintenance and enhances reliability. Functional Programming with C# takes you on an exhaustive journey into functional programming within C#. The book familiarizes you with the core principles and benefits of functional programming, juxtaposing it with imperative and object-oriented paradigms. You'll get to grips with the functional features of C# and learn to write expressive, modular code through expressions, pure functions, and higher-order functions. As you advance, you'll explore advanced concepts such as error handling and asynchronous programming, which will equip you with the practical skills that you need to tackle real-world challenges. By comparing functional programming in C# with other languages and providing additional resources, this book ensures that you're well-equipped to continue your journey toward mastery in functional programming. By the end of this book, you'll have gained a thorough understanding of functional programming techniques, such as recursion, memoization, and composition, empowering you to write robust, maintainable code.
All prices
More details
Other editions
Additional editions

Content
- Cover
- Title Page
- Copyright and Credits
- Dedication
- Contributors
- Table of Contents
- Preface
- Part 1: Foundations of Functional Programming in C#
- Chapter 1: Getting Started with Functional Programming
- Functional versus imperative versus object-oriented programming
- Imperative programming
- Object-oriented programming
- Functional programming
- Blending paradigms
- 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
- Example of expressions
- Example of statements
- Key differences between expressions and statements
- Guided exercise - finding expressions and statements in sample code
- How to use expressions for clear and simple code
- The power of expressions - improving readability and maintainability
- Techniques to convert statements to expressions
- Guided exercise - refactoring code using expressions
- Lambda expressions, expression-bodied members, and anonymous methods
- What are lambda expressions?
- Multiple parameters in lambda expressions
- Lambda expressions evolution
- Understanding anonymous methods
- How do anonymous methods work?
- When to use anonymous methods
- Practical examples - applying these features in real code
- Expression-bodied members
- Exercise - implementing lambda expressions and anonymous methods
- Expression trees and how to use them to manipulate expressions at runtime
- Building and manipulating expression trees
- Creating and manipulating complex expression trees
- Querying data with expression trees - LINQ and beyond
- Guided exercise - constructing and manipulating expression trees
- Problem sets and exercises
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Exercise 1
- Exercise 2
- Exercise 3
- 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
- Examples of pure functions
- The benefits of pure functions
- Comparisons of pure functions and non-pure functions
- Side effects
- Common sources of side effects
- Consequences of side effects
- Strategies to minimize side effects
- Favor immutability
- Use readonly and const
- Use functional programming principles
- Encapsulate side effects
- Marking pure functions with the Pure attribute
- Understanding the Pure attribute in C#
- The benefits of marking functions as pure
- Caveats and considerations when using the Pure attribute
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Exercise 1
- Exercise 2
- Exercise 3
- 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
- A quick look back - C# and null
- Common mistakes and the troubling NullReferenceException
- Not all bad - the value of null
- Embracing honesty with nullable reference types
- What are nullable reference types?
- Transitioning to NRTs
- Enabling nullable reference types
- Disabling nullable reference types
- Warnings and annotations options
- The bigger picture - project-wide settings
- Returning with intention
- The distinction between UserProfile and UserProfile?
- Honoring a function's contract
- Demanding honesty from function inputs
- Nullable reference types to the rescue
- Using preconditions and contracts
- Using built-in checks
- The power of explicit non-null inputs
- Pattern matching and nullable types
- Pattern matching with nullable types
- Switch expressions with property patterns
- Ensuring clarity with nullable types
- The null object pattern
- The problem with null checks
- The null object solution
- Advantages
- Limitations and considerations
- Beyond null - using Option
- A brief introduction to Option
- Advantages of the Option type over nullable types
- The interplay of Option and nullable types
- Practical scenarios - handling nulls effectively
- Case study - managing YouTube videos
- Case study - managing different video types
- Case study - working with non-existing objects
- The impact of handling nulls in real-world scenarios
- 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
- Strategies for migrating existing code bases to adopt nullable reference types and Option
- Common pitfalls and how to navigate them
- Testing strategies for null and Option handling
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Exercise 1
- Exercise 2
- Exercise 3
- 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#
- try-catch blocks
- Exceptions
- The Result type
- Implementing the Result type
- Using the Result type
- Railway-Oriented Programming (ROP)
- The essence of Bind
- Chaining operations with Bind
- Composable error handling with ROP
- Handling diverse error types
- Benefits of isolation
- Extending ROP for asynchronous operations
- Designing your own error-handling mechanisms
- Use factory methods for creation
- Extend with Bind
- Customize error types
- Leverage extension methods
- Integration with existing code
- Always iterate and refine
- Practical tips for functional error handling
- Avoid null with options
- Logging errors
- Two strategies To replace exceptions
- Anticipate errors - make it predictable
- Embrace composition
- Educate your team
- Traditional versus functional error handling comparison
- The traditional way
- The functional way
- Comparative analysis
- Making the shift
- Patterns and anti-patterns in functional error handling
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Exercise 1
- Exercise 2
- Exercise 3
- Summary
- Chapter 6: Higher-Order Functions and Delegates
- Task 1 - Sorting function
- Task 2 - Customized calculations
- Task 3 - Comparison
- Understanding higher-order functions
- The power of higher-order functions in functional programming
- Creating versatile code with fewer errors
- Supporting a more declarative coding style
- Delegates, actions, funcs, and predicates
- Delegates
- Actions
- Funcs
- Predicates
- Callbacks, events, and anonymous methods
- The role of delegates in events
- Delegates and anonymous methods
- Harnessing LINQ methods as higher-order functions
- Filtering
- Data transformation
- Data aggregation
- Case study - putting it all together
- Step-by-step walk-through and analysis of the code
- Best practices and common pitfalls
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Exercise 1
- Exercise 2
- Exercise 3
- Summary
- Chapter 7: Functors and Monads
- Task 1 - Functor usage
- Task 2 - Applicative functor
- Task 3 - Monad usage
- What's a functor?
- Identity law
- Composition law
- Creating our own functor
- Functor benefits
- Applicative functors
- The Apply method implementation
- Applicative functor laws
- Monads
- The Bind method
- Monad laws
- Utilizing monads
- Key takeaways
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Exercise 1
- Exercise 2
- Exercise 3
- 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
- Simple recursion
- Tail recursion
- Challenges of recursion
- Stack overflow risk
- Default stack size and limitations
- Performance considerations
- Leveraging C# features for recursion
- Local functions
- Pattern matching
- Advanced recursive patterns
- Mutual recursion
- Memoization
- Comparison with iterative solutions
- Readability
- Performance
- Recursion in asynchronous programming
- Explaining asynchronous recursion under the hood
- Synchronous versus asynchronous recursion
- Scenarios for asynchronous recursion
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Exercise 1
- Exercise 2
- Exercise 3
- 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
- Standard approach
- Curried approach
- Step-by-step implementation of currying
- Use cases
- Configuration settings
- Event handling
- Partial application
- Asynchronous programming
- Partial application
- Areas for partial application
- Example of partially applying a function for configuration settings
- Standard rendering function
- Partially applied function
- Challenges and limitations
- Exercises
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Solution 1
- Solution 2
- Solution 3
- 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
- Exercise 1
- Exercise 2
- Exercise 3
- Solutions
- Solution 1
- Solution 2
- Solution 3
- 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
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.