Functional Programming in Javascript

Functional Programming in Javascript

gain an advanced understanding of the mathematics behind functional programming

About the Book

Note: This is not an Introductory book in JavaScript. The reader must have at least a couple of years of JavaScript experience. Please read the Sample pdf to get an idea about the content of the book, also browse some of the JsFiddles here :

  • https://github.com/dimitris-papadimitriou-chr/FunctionalJsWithCategories

Paperback is now available in Amazon Functional Programming in JavaScript

One of the main reasons for this book is to transfer in the community of object-oriented developers some of the ideas and advancements happening to the functional community side and the mathematical foundations behind them.

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 JS fiddle JavaScript code [https://jsfiddle.net/user/functionalCategories/fiddles/], which you can run fork and modify, in order to deepen your understanding of each section.

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

What you will learn:

  1. Go beyond the Array.map by understanding how to generalize the idea of map to Functors like Maybe, Either and Promises, etc.
  2. Go beyond the idea of Array.reduce 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, Continuations, and Promises to chain computations.
  5. Get a deep understanding of recursive methods and learn how to define recursive methods on algebraic data structures.

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

  • Contents
  • About this book coding conventions 8
  • Type Safety 8
  • Fantasy land 10
    • 1.1 Categories ... 13
    • 1.2 Essential Lambda calculus with Js ... 15
    • 1.3 SKI combinators ... 16
  • 2 Basic Concepts
    • 2.1 Basic Type Theory -[Optional] ... 19
    • 2.2 Why Types are important ... 19
    • 2.3 Variable Types ... 20
    • 2.4 Function types ... 20
    • 2.4.1 Functions with a single argument ... 20
    • 2.4.2 Functions with multiple arguments ... 21
    • 2.5 Generic Types ... 21
  • 3 More Functional Concepts
    • 3.1 Object literal notion ... 22
  • Important points 22
    • 3.2 Pure functions and Side effects ... 23
    • 3.3 Mutability ... 24
    • 3.4 Immutability with Object literals ... 25
  • 4 The Pillars
    • 4.1 Monoids ... 27
    • 4.2 Function composition as a monoid ... 30
    • 4.3 Decorator Design pattern - Functional Perspective ... 31
    • 4.4 Adapter Design pattern - Functional Perspective ... 33
    • 4.5 Folding monoids ... 33
    • 4.6 folding functions under composition ... 35
    • 4.7 Composing monoids ... 36
    • 4.8 Products of Monoids ... 39
    • 4.9 Predicate monoidal Composition ... 40
    • 4.10 Specification Pattern - Functional Perspective ... 42
    • 4.11 Monoid homomorphisms and Parallelism ... 44
    • 4.12 Parallelism using Web Workers ... 47
    • 4.13 Higher-order functions ... 50
    • 4.14 Strategy Design pattern - Functional Perspective ... 50
    • 4.15 Currying and partial application ... 52
  • 5 Algebraic Data Types
    • 5.1 The product structure: ... 57
    • 5.1.1 Universal Construction ... 58
    • 5.1.2 Introduction / Elimination ... 59
    • 5.2 The co-product (aka Union) structure: ... 60
    • 5.2.1 Introduction / Elimination ... 63
    • 5.3 Extending Union Types ... 64
    • 5.3.1 Adding Pattern Matching extensions to Union Types ... 64
    • 5.3.2 Rewriting Union Type methods with match ... 65
    • 5.4 One ... 66
    • 5.5 Recursive Algebraic Types ... 67
    • 5.5.1 Rewriting map with match ... 68
    • 5.5.2 On the value of the symbolic representation ... 69
    • 5.5.3 Adding Pattern Matching extension to native array ... 70
  • 6 Functors
    • 6.1 The Identity Functor ... 75
    • 6.2 Commutative Diagrams ... 77
    • 6.3 The Functor Laws ... 78
    • 6.4 Decorator design pattern - Functor ... 81
    • 6.5 Pattern Matching ... 82
    • 6.6 Pattern match - Compositions ... 83
    • 6.7 Pattern matching - Inheritance ... 84
    • 6.7.1 Pattern matching in functional libraries ... 87
    • 6.8 Id Functor on the Fly ... 87
    • 6.9 The Basic Functor Mechanics ... 88
    • 6.10 IO Functor, a Lazy Id Functor ... 89
    • 6.10.1 Lazy as Functor ... 90
    • 6.10.2 IO Functor ... 91
    • 6.11 Reader Functor ... 92
    • 6.12 Maybe Functor ... 94
  • Dealing with null - Null object Design pattern 94
    • 6.13 The Null Object Design pattern ... 95
    • 6.14 The Functional equivalent - Maybe as Functor ... 96
    • 6.15 Pattern matching ... 97
    • 6.16 Refactoring the imperative example using Maybe ... 97
    • 6.17 Maybe Functor Example ... 98
    • 6.18 Maybe -React.js with Folktale.js [Optional] ... 100
    • 6.19 Either ... 102
    • 6.20 The Imperative equivalent to Either ... 104
    • 6.21 Either -Sanctuary.js ... 105
    • 6.22 Either -Folktale.js ... 107
    • 6.23 Either Functor Example ... 108
    • 6.24 Either Functor Example- Imperative Equivalent ... 109
    • 6.25 Either Functor Example - Sanctuary.js ... 109
    • 6.26 Either -React.js with Folktale.js [Optional] ... 110
    • 6.27 Generalizing Either - biMap / mapLeft ... 111
    • 6.28 Promise as Functor ... 112
    • 6.29 The Promise - functor laws ... 114
    • 6.30 Promise Functor Example ... 115
    • 6.31 Maybe Functor Example ... 116
    • 6.32 Composing Functors ... 118
    • 6.33 Transformer Types ... 119
    • 6.34 Promise-Maybe Composition Example ... 120
    • 6.35 Transformer operators ... 121
    • 6.36 Transformer Adapters (aka monad transformers) ... 121
    • 6.37 Promise-Either Composition Example ... 122
    • 6.38 Promise-Either -React.js with Folktale.js [Optional] ... 124
    • 6.39 Try- Using Either for exception handling ... 125
    • 6.40 Try Type Refactoring Example ... 128
    • 6.41 Functors from Algebraic Data Types ... 129
    • 6.42 Applicative Functor ... 132
    • 6.43 Reader Applicative Functor ... 133
    • 6.44 Composing Applicatives ... 135
  • 7 Natural Transformations
    • 7.1.1 Natural Transformation Between Maybe and Either ... 139
  • 8 Monads
    • 8.1 The Identity Monad ... 142
    • 8.2 The Array Monad ... 143
    • 8.3 Monad laws for Identity Monad ... 144
    • 8.4 Monads -Kleisli Composition ... 145
    • 8.5 Monad laws following Kleisli formulation ... 146
    • 8.6 Maybe Monad ... 147
    • 8.7 Maybe Monad Example ... 149
    • 8.8 Either Monad ... 151
    • 8.9 Either Monad Example ... 152
    • 8.10 Promise as monad ... 153
    • 8.11 Promise monad example ... 154
    • 8.12 Fluture.js ... 155
    • 8.13 Maybe-Promise monad combination ... 157
    • 8.14 Either-Promise monad combination ... 158
    • 8.15 Either-Promise monad Example-React.js with Folktale.js ... 159
    • 8.16 Validation type ... 161
    • 8.17 State Monad ... 161
    • 8.18 Traversing with State ... 164
    • 8.19 Reader Monad ... 166
    • 8.20 IO monad ... 168
    • 8.21 Writer Monad ... 169
    • 8.22 Traversing with Writer ... 170
  • 9 Catamorphisms Again
    • 9.1 A brief mentioning of F-algebras ... 172
    • 9.2 Catamorphisms ... 173
    • 9.3 Initial algebra ... 176
    • 9.3.1 F-Algebras Homomorhisms ... 176
    • 9.4 Catamorphisms for Trees ... 178
    • 9.5 Reversing a Tree ... 179
    • 9.6 Catamorphisms with the Visitor Design pattern ... 180
    • 9.6.1 F-Coalgebra ... 181
    • 9.7 A brief mentioning of Anamorhisms ... 181
    • 9.7.1 Corecursion ... 182
    • 9.7.2 JavaScript Generators ... 183
    • 9.7.3 A brief mentioning of Hylomorphisms ... 184
    • 9.7.4 Hylomorhism example: Mergesort ... 186
    • 9.8 Fold relation with Cata ... 187
  • 10 Traversable
    • 10.1 Traversable Array with Either applicative for validation ... 190
    • 10.2 Traversable Algebraic data structures ... 191
    • 10.3 Identity Functor Traversable ... 192
    • 10.4 Traversing with The Promise - Parallel ... 193
    • 10.5 Applicative Reader Isomorphism with the Interpreter Design pattern ... 194
    • 10.5.1 A Catamorphism implementation ... 196
    • 10.5.2 Object Oriented Interpreter Pattern implementation ... 196
    • 10.6 Composing Traversables ... 197
    • 10.7 Foldable ... 198
    • 10.7.1 FoldMap ... 199
    • 10.7.2 Filterable structures ... 201
    • 10.8 Fold and FoldMap Derivation from Catamorphism ... 201
    • 10.9 Traverse Derivation from Catamorphism ... 203
    • 10.9.1 Mixins ... 203
    • 10.9.2 Composing Foldables ... 204
    • 10.9.3 Iterators ... 205
  • 11 Comonads
    • 11.1 The Identity Comonad ... 207
    • 11.2 Co-Monad laws for Identity Co-Monad ... 208
    • 11.3 Co-Kleisli Composition ... 209
    • 11.3.1 Co-Monad laws following Co-Kleisli formulation ... 210
    • 11.4 Store Comonad ... 210
    • 11.5 Lazy ... 211
    • 11.6 Pair Comonad ... 212
    • 11.7 Spatial Comonad and the Game of Life ... 215
    • 11.8 Stream Comonad ... 218
    • 11.9 Tree Comonad ... 219
    • 11.9.1 Tree annotation example ... 220
  • 12 F-Algebras
    • 12.1 F-Algebras Homomorhisms ... 226
    • 12.2 Initial Algebras ... 228
  • 13 F -Coalgebra
    • 13.1 Catmorphisms ... 230

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