Functional Programming in C#

Retired

This book is no longer available for sale.

Functional Programming in C#

with Categories

About the Book

Note: This is not an Introductory book in C#. The reader must have at least a couple of years of C# experience. Please read the Sample pdf to get an idea about the content of the book.

This book contains the full content of the book "Practical functional C#" Don't buy both books.

This book is focused on the practical use of language-ext functional library in C#. We will break down the Sample projects and see where functional programming fits in modern Web Application development using Asp.Net Core and Domain Driven Design .

The paperback is also Available at Amazon Functional Programming in C#

Book Description

This book goes beyond the introductory ideas of functional programming, while progressively building on practical examples in a comprehensive manner.

Each example is accompanied by online autonomous NET Fiddles :

[https://dotnetfiddle.net/Authors/127896]

[https://dotnetfiddle.net/Authors/120678/dimitrispapadim]

which you can run fork and modify, in order to deepen your understanding of each section.

GitHub repositories

[FunctionalCSharpWithCategories]

[https://github.com/dimitris-papadimitriou-chr/Practical-Functional-CSharp]

By the end of this book, you will have a deep understanding of the ideas behind functional C# that will allow you to program in a functional style with confidence. Also, you will have an overview of how famous functional libraries have implemented different functional concepts.

What you will learn:

  1.    Go beyond the List.Select by understanding how to generalize the idea of map to Functors like Maybe, Either and Task<T>, etc.
  2.    Go beyond the idea of List.Aggregate by understanding the concepts of fold and catamorphism over data structures.
  3.    Learn how to combine functional ideas in order to solve practical problems.
  4.    Use Monads like Maybe, Either, IO, and Task to chain computations.
  5.    Get a deep understanding of recursive methods and learn how to define recursive methods on algebraic data structures.
  • Share this book

  • Categories

    • C#
    • Functional Programming
  • Installments completed

    5 / 5

  • Feedback

    Email the Author(s)

About the Author

dimitris papadimitriou
dimitris papadimitriou

I have more than 14 years’ experience as full stack developer and Architect.

I Like to stay in touch with all the latest developments in the field, as well as the foundational Theoretical Computer science and algorithm Analysis and Design

https://www.linkedin.com/in/dimitrispapadimitriou/

Table of Contents

  • 1 Language Functional support
    • 1.1 Pure functions and Side effects ... 8
    • 1.2 Evolution of Delegates in C# ... 11
    • 1.3 Named parameters and Tuples ... 14
    • 1.3.1 Lambda expressions and tuples ... 16
    • 1.4 Extension Methods ... 18
    • 1.5 Using static directive ... 19
    • 1.6 LINQ support for custom Data structures ... 20
    • 1.7 Currying and partial application ... 21
  • 2 Algebras of Programming
    • 2.1 Categories ... 24
    • 2.2 Monoids ... 26
    • 2.2.1 Folding monoids ... 28
    • 2.2.2 Monoid homomorphisms and Parallelism ... 30
  • 3 Algebraic Data Types
    • 3.1 The product structure: ... 35
    • 3.1.1 Introduction / Elimination ... 36
    • 3.2 The Co-Product (aka Union) structure ... 37
    • 3.2.1 Introduction / Elimination ... 39
    • 3.3 Extending Union Types ... 39
    • 3.3.1 Adding Pattern Matching extensions to Union Types ... 40
    • 3.3.2 Rewriting Union Type methods with MatchWith ... 40
    • 3.3.3 The C#8.0 Pattern matching Feature ... 41
    • 3.4 One ... 42
    • 3.5 Recursive Algebraic Types ... 43
    • 3.5.1 Rewriting Map with MatchWith ... 44
    • 3.5.2 On the value of the symbolic representation ... 45
    • 3.5.3 Adding Pattern Matching extension to Native List ... 45
  • 4 Functors
    • 4.1 The Identity Functor ... 49
    • 4.2 Commutative Diagrams ... 51
    • 4.3 The Functor Laws ... 51
    • 4.4 Pattern Matching ... 53
    • 4.5 Id Functor on the Fly ... 57
    • 4.5.1 Some more Isomorphisms ... 58
    • 4.5.2 The Basic Functor Mechanics ... 60
    • 4.6 Extending Task to Functor ... 60
    • 4.7 IO Functor, a Lazy Id Functor ... 62
    • 4.7.1 Func Delegates as Functor ... 63
    • 4.7.2 IO Functor ... 63
    • 4.8 Reader Functor ... 64
    • 4.9 LINQ Native Query syntax: Functor Support ... 65
    • 4.10 Maybe Functor aka Option ... 66
    • 4.10.1 Dealing with null ... 66
    • 4.10.2 The Null Object Design pattern ... 67
    • 4.10.3 The Functional equivalent - Maybe as Functor ... 68
    • 4.10.4 Maybe Functor Example ... 71
    • 4.10.5 Maybe in Language-ext / Option ... 72
    • 4.10.6 Maybe Functor Example With language-ext Option ... 72
    • 4.10.7 C# 8. pattern matching Support for Option ... 73
    • 4.10.8 Folding Maybe ... 74
    • 4.10.9 Using the Linq syntax ... 74
    • 4.11 Either Functor ... 75
    • 4.11.1 Pattern matching for Either ... 76
    • 4.11.2 Using C# 8.0 pattern matching ... 77
    • 4.11.3 Either Functor Example ... 77
    • 4.11.4 C# 8. pattern matching Support for language-ext Either ... 79
    • 4.11.5 Using Either for exception handling ... 79
    • 4.12 Explicitly Compositing Functor ... 82
    • 4.13 Combining Task and Option – Task> ... 84
    • 4.14 Combining Task and Option – OptionAsync ... 86
    • 4.15 Combining Task and Either – Task> ... 87
    • 4.16 Combining Task and Either – EitherAsync<> ... 89
    • 4.17 Functors from Algebraic Data Types ... 90
    • 4.18 Applicative Functors ... 93
    • 4.19 Reader Applicative Functor ... 94
  • 5 Monads
    • 5.1 The List Monad ... 100
    • 5.2 The Identity Monad ... 101
    • 5.2.1 Monad laws for Identity Monad ... 101
    • 5.3 Maybe Monad ... 102
    • 5.3.1 Using language-ext Option ... 104
    • 5.3.2 Using the LINQ syntax with Option Monad ... 105
    • 5.4 Either Monad ... 106
    • 5.4.1 Using Either Monad for exception handling ... 107
    • 5.4.2 Using language-ext Either<> ... 108
    • 5.4.3 Using the LINQ syntax with Either Monad ... 109
    • 5.4.4 Using Either for Validation ... 109
    • 5.5 Validation Monad ... 110
    • 5.5.1 Using Validation.Apply to Collect validations ... 111
    • 5.6 Task as Monad ... 112
    • 5.7 The Task - Option Monad Combination -Task> ... 113
    • 5.8 The Task - Option Monad Combination -OptionAsync<> ... 115
    • 5.9 The Task - Either Monad Combination -Task> ... 117
    • 5.10 The Task - Either Monad Combination -EitherAsync<> ... 119
  • 6 Catamorphisms Again
    • 6.1 A brief mentioning of F-algebras ... 121
    • 6.2 Catamorphisms ... 122
    • 6.3 Initial algebra ... 123
    • 6.3.1 F-Algebras Homomorhisms ... 124
    • 6.4 Catamorphisms for Trees ... 125
    • 6.5 Catamorphisms with the Visitor Design pattern ... 127
    • 6.5.1 The Base Functor of the List ... 128
    • 6.5.2 A brief mentioning of Anamorhisms ... 131
    • 6.5.3 F-Coalgebra ... 133
    • 6.5.4 Corecursion ... 133
    • 6.5.5 A brief mentioning of Hylomorphisms ... 134
    • 6.5.6 Hylomorphism example: Mergesort ... 136
    • 6.6 Fold’s relation to Cata ... 137
  • 7 Traversable
    • 7.1 Traversable Array with Either applicative for validation ... 141
    • 7.2 Traversable Algebraic data structures ... 142
    • 7.3 Traversing with The Task aka Parallel ... 143
    • 7.4 Applicative Reader Isomorphism with the Interpreter Design pattern ... 145
    • 7.4.1 Standard Catamorphism implementation ... 146
    • 7.4.2 Reader applicative implementation ... 147
    • 7.4.3 Object Oriented Interpreter Pattern implementation ... 148
    • 7.5 Explicitly Composing Traversables ... 149
    • 7.6 Foldable ... 150
    • 7.6.1 FoldMap ... 151
    • 7.6.2 Explicitly Composing Foldables ... 153
  • 8 A Clean Functional Architecture Example
    • 8.1 Download and Setup the Project ... 154
    • 8.1.1 Clean Architecture with .NET core ... 155
    • 8.2 A Functional Applications Architecture ... 157
    • 8.3 The Contoso Clean Architecture with .NET core and language-ext ... 158
    • 8.4 Web Api ... 159
    • 8.5 Domain Model ... 161
    • 8.5.1 Repositories ... 164
    • 8.6 CQRS ... 164
    • 8.7 CQRS and MediatR ... 166
    • 8.7.1 The Command Pattern with a Mediator ... 166
    • 8.7.2 Query Workflow ... 169
    • 8.7.3 Command Workflow ... 173

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub