Composing Software an Exploration of Functional Programming and Object Composition in Javascript
Total Page:16
File Type:pdf, Size:1020Kb
Composing Software An Exploration of Functional Programming and Object Composition in JavaScript Eric Elliott This book is for sale at http://leanpub.com/composingsoftware This version was published on 2019-02-24 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2017 - 2019 Eric Elliott Contents Thank You ................................................... 1 Composing Software: An Introduction ................................. 2 You Compose Software Every Day .................................. 3 Conclusion ................................................. 10 The Dao of Immutability (The Way of the Functional Programmer) .............. 11 Forward .................................................. 11 The Rise and Fall and Rise of Functional Programming (Composable Software) ....... 13 The Rise of Functional Programming ................................. 13 The Fall of Functional Programming ................................. 16 The Rise of Functional Programming ................................. 16 Functional Programming Has Always Been Alive and Well ................... 17 Why Learn Functional Programming in JavaScript? ........................ 18 Pure Functions ................................................ 25 What is a Function? ........................................... 25 Mapping .................................................. 25 Pure Functions .............................................. 26 The Trouble with Shared State ..................................... 27 Same Input, Same Output ........................................ 28 No Side Effects .............................................. 29 Conclusion ................................................. 32 What is Functional Programming? .................................... 33 Pure Functions .............................................. 34 Function Composition .......................................... 34 Shared State ................................................ 34 Immutability ............................................... 37 Side Effects ................................................ 38 Reusability Through Higher Order Functions ............................ 38 Containers, Functors, Lists, and Streams ............................... 39 Declarative vs Imperative ........................................ 40 CONTENTS Conclusion ................................................. 41 A Functional Programmer’s Introduction to JavaScript ...................... 43 Expressions and Values ......................................... 43 Types .................................................... 44 Destructuring ............................................... 45 Comparisons and Ternaries ....................................... 46 Functions .................................................. 47 Currying .................................................. 51 Function Composition .......................................... 53 Arrays ................................................... 54 Method Chaining ............................................. 54 Conclusion ................................................. 55 Higher Order Functions ........................................... 56 Curry and Function Composition .................................... 59 What is a curried function? ....................................... 59 What is a partial application? ..................................... 60 What’s the Difference? ......................................... 60 What is point-free style? ........................................ 60 Why do we curry? ............................................ 61 Trace .................................................... 63 Curry and Function Composition, Together ............................. 64 Conclusion ................................................. 69 Abstraction & Composition ........................................ 71 Abstraction is simplification. ...................................... 72 Abstraction in Software ......................................... 72 Abstraction through composition ................................... 73 How to Do More with Less Code ................................... 74 Conclusion ................................................. 75 Reduce ................................................... 75 Reduce is Versatile ............................................ 76 Conclusion ................................................. 80 Functors & Categories ............................................ 81 Why Functors? .............................................. 82 Functor Laws ............................................... 86 Category Theory ............................................. 87 Build Your Own Functor ........................................ 89 Curried Map ................................................ 90 Conclusion ................................................. 91 CONTENTS Monads ..................................................... 92 You’re probably already using monads. ............................... 94 What Monads are Made of ....................................... 99 Building a Kleisli Composition Function ...............................102 The Monad Laws .............................................105 Conclusion .................................................109 The Forgotten History of OOP ...................................... 110 The Big Idea ................................................111 The Essence of OOP ...........................................113 What OOP Doesn’t Mean ........................................114 What is an object? ............................................116 We’ve lost the plot. ............................................117 Object Composition ............................................. 119 What is Object Composition? .....................................120 Three Different Forms of Object Composition . 121 Notes on Code Examples ........................................121 Aggregation ................................................122 Concatenation ...............................................124 Delegation .................................................125 Conclusion .................................................127 Factory Functions ............................................... 129 Literals for One, Factories for Many .................................130 Returning Objects ............................................131 Destructuring ...............................................131 Computed Property Keys ........................................132 Default Parameters ............................................133 Type Inference ..............................................134 Factory Functions for Mixin Composition ..............................136 Conclusion .................................................138 Functional Mixins .............................................. 140 Motivation .................................................140 What are mixins? .............................................141 What is functional inheritance? ....................................142 What is a functional mixin? ......................................143 Composing Functional Mixins .....................................144 When to Use Functional Mixins ....................................145 Caveats ...................................................145 Conclusion .................................................150 Why Composition is Harder with Classes ............................... 151 CONTENTS The Delegate Prototype .........................................151 The .constructor Property .......................................153 Class to Factory is a Breaking Change ................................156 Composable Custom Data Types ..................................... 162 You can do this with any data type ..................................165 Composable Currency ..........................................165 Lenses ...................................................... 167 Why Lenses? ...............................................168 Background ................................................168 Lens Laws .................................................169 Composing Lenses ............................................171 Transducers .................................................. 174 Why Transducers? ............................................175 Background and Etymology ......................................177 A Musical Analogy for Transducers .................................179 Transducers compose top-to-bottom. .................................185 Transducer Rules .............................................185 Transducing ................................................187 The Transducer Protocol ........................................189 Conclusion .................................................190 Elements of JavaScript Style ........................................ 192 1. Make the function the unit of composition. One job for each function. 192 2. Omit needless code. ..........................................193 3. Use active voice. ............................................197 4. Avoid a succession of loose statements. ..............................198 5. Keep related code together. .....................................200 6. Put statements and expressions in positive form. 200 7. Use parallel