O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functional C#

Book Description

Uncover the secrets of functional programming using C# and change the way you approach your applications forever

About This Book

  • This book focuses on the functional paradigm of C#, which will give you a whole new angle on coding with C#
  • It illustrates the advantages that functional programming brings to the table and the associated coding benefits
  • This practical guide covers all the aspects of functional programming and provides solutions that can be applied in business scenarios

Who This Book Is For

This book is suitable for C# developers with basic prior knowledge of C# and with no functional programming experience at all.

What You Will Learn

  • Develop an application using the functional approach
  • Implement unit testing to functionally program code
  • Create efficient code using functional programming
  • Work through a LINQ query so you can work with data
  • Compose asynchronous programs to create a responsive application
  • Use recursion in function programming in order to simplify code
  • Optimize the program code using Laziness and Caching Techniques

In Detail

Functional programming makes your application faster, improves performance, and increases your productivity. C# code is written at a higher level of abstraction, so that code will be closer to business requirements, abstracting away many low-level implementation details.

This book bridges the language gap for C# developers by showing you how to create and consume functional constructs in C#. We also bridge the domain gap by showing how functional constructs can be applied in business scenarios. We’ll take you through lambda expressions and extension methods, and help you develop a deep understanding of the concepts and practices of LINQ and recursion in C#.

By the end of the book, you will be able to write code using the best approach and will be able to perform unit testing in functional programming, changing how you write your applications and revolutionizing your projects.

Style and approach

This book takes a pragmatic approach and shows you techniques to write better functional constructs in C#. We’ll also show you how these concepts can be applied in business scenarios.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Functional C#
    1. Functional C#
    2. Credits
    3. About the Author
    4. Acknowledgments
    5. About the Reviewer
    6. www.PacktPub.com
      1. Why subscribe?
    7. Customer Feedback
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Tasting Functional Style in C#
      1. Introducing functional programming
        1. Understanding definitions, scripts, and sessions
        2. Using substitution and simplification to evaluate the expression
        3. Understanding the functions used for functional programming
          1. Forming the definition
          2. Currying
      2. Comparison between functional and imperative programming
      3. Preparing the C# compiler
      4. Concepts of functional programming
        1. First-class and higher-order functions
          1. Function types
          2. Function values
        2. Pure functions
        3. Recursive functions
      5. Feeling functional in C#
        1. Using mathematical concept to understand functional approach
        2. Applying tuple for functional C#
        3. Currying in C#
        4. Pipelining
        5. Method chaining
      6. Transforming imperative code to functional code
        1. The imperative code approach
        2. The functional code approach
          1. The GenerateOrderedList() method
          2. The Main() method
      7. The advantages and disadvantages of functional programming
      8. Summary
    10. 2. Walkthrough Delegates
      1. Introducing delegates
        1. Simple delegates
        2. Multicast delegates
          1. Using the Delegate.Combine() and Delegate.Remove() methods
          2. Using += and -= operators
      2. Built-in delegates
        1. Generic delegates
        2. The Action and Func delegates
      3. Distinguishing variance in delegates
        1. Covariance
        2. Contravariance
      4. Summary
    11. 3. Expressing Anonymous Methods with Lambda Expressions
      1. Getting to know anonymous methods
        1. Creating anonymous methods
        2. Using an anonymous method as an argument
        3. Writing anonymous methods - some guidelines
        4. Advantages of the anonymous methods
      2. Lambda expressions
        1. Transforming an anonymous method to a lambda expression
        2. Creating a delegate type using lambda expresions
        3. Expression trees and lambda expressions
      3. Subscribing for events using lambda expressions
        1. Using the event keyword
        2. Using EventHandler or EventHandler<T>
      4. The advantages of using lambda expression in functional programming
        1. First-class functions
        2. Closure
      5. Summary
    12. 4. Extending Object Functionality with Extension Methods
      1. Getting closer to extension methods
        1. Creating an extension method
        2. Extension methods in the code IntelliSense
      2. Calling extension methods in the other assemblies
        1. Referencing a namespace
        2. Piggybacking a namespace
      3. Leveraging the interface, collection, and object
        1. Extending the interface
        2. Extending the collection
        3. Extending an object
      4. Advantages of using extension methods in functional programming
      5. Limitations of the extension method
        1. Extending a static class
        2. Modifying the method implementation in the existing class or type
      6. Summary
    13. 5. Querying Any Collection Easily with LINQ
      1. Getting started with LINQ
      2. Deferring LINQ execution
      3. Choosing between fluent syntax and query expression syntax
        1. Understanding the LINQ fluent syntax
        2. Understanding the LINQ query expression syntax
      4. Enumerating standard query operators
        1. Filtering
        2. Projection
        3. Joining
        4. Ordering
        5. Grouping
        6. The set operation
        7. Conversion methods
        8. Element operation
      5. Summary
    14. 6. Enhancing the Responsiveness of the Functional Program with Asynchronous Programming
      1. Building a responsive application
        1. Running a program synchronously
        2. Applying threads in the program
        3. Creating threads using thread pool
      2. The asynchronous programming model pattern
        1. Using the synchronous Read() method
        2. Using the BeginRead() and EndRead() methods
        3. Adding LINQ to the BeginRead() method invocation
      3. The task-based asynchronous pattern
        1. Introducing the Task and Task<TResult> classes
        2. Applying a simple TAP model
        3. Using the WhenAll() extension method
        4. Wrapping an APM into a TAP model
      4. Asynchronous programming with the async and await keywords
      5. Asynchronous functions in functional programming
      6. Summary
    15. 7. Learning Recursion
      1. Exploring recursion
        1. Working of the recursive routine
        2. Refactoring an iteration to the recursion
      2. Using tail recursion
        1. Accumulator-passing style
        2. Continuation-passing style
        3. Indirect recursion over direct recursion
      3. Recursion in a functional approach using LINQ Aggregate
        1. Delving into the Aggregate method
      4. Summary
    16. 8. Optimizing the Code using Laziness and Caching Techniques
      1. Introduction to laziness
        1. Lazy enumeration
        2. Lazy evaluation
        3. Nonstrict evaluation
        4. Lazy initialization
        5. The advantages and disadvantages of being lazy
      2. Caching expensive resources
        1. Performing initial computation
        2. Memoization
      3. Summary
    17. 9. Working with Pattern
      1. Dissecting pattern matching in functional programming
        1. Transforming data using pattern matching
        2. Switching for pattern matching
        3. Simplifying pattern matching
        4. Welcoming the coming of pattern matching feature in C# 7
      2. Introducing Monad as a design pattern
        1. Creating the Monadic M<T> type
        2. Implementing the generic data type to Monad
        3. Implementing Monad to Lazy<T> and Task<T>
        4. Rules of the Monad pattern
      3. Summary
    18. 10. Taking an Action in C# Functional Programming
      1. Developing functional programming in Windows forms
        1. Creating the code behind a form
        2. Creating the engine code in an imperative approach
          1. Preparing class properties
          2. Constructing the constructor
          3. Clearing the properties
          4. Appending the number to the display box
          5. Preparing the mathematical operation
          6. Formatting the input
          7. Solving the calculation
          8. Calculating the additional operation
        3. Creating the engine code in the functional approach
          1. Adding several new properties
          2. Simplifying the pattern matching
          3. Assigning the properties
          4. Constructing the class by clearing the properties
          5. Appending the inputted number to the text box
          6. Preparing the operation
          7. Formatting the input
          8. Solving the calculation
          9. Calculating the additional operation
      2. Summary
    19. 11. Coding Best Practice and Testing the Functional Code
      1. Coding best practices in functional C#
        1. Preventing dishonest signatures
        2. Refactoring a mutable class into an immutable one
        3. Avoiding mutability and temporal coupling
        4. Dealing with the side-effects
      2. Separating the code from domain logic and mutable shell
        1. Examining the code containing side-effects
        2. Refactoring the AddRecord() method
        3. Refactoring the RemoveRecord() method
        4. Running domain logic in unit testing
          1. Testing the AddRecord() method
          2. Testing the RemoveRecord() method
          3. Executing the test
        5. Adding the mutable shell into code
      3. Summary