Watch The Power of Composition
By "composition," we mean, "Assemble a few low-level reusable pieces into a higher-level piece." Here are some examples:
- (Classic example) Legos. Using small blocks of plastic, people can create all sorts of interesting things.
- Furniture. Using wood, metal, fabric, glass, and nails, people can create tables, chairs, desks, cabinets, etc.
Composition makes FP code easy to refactor because we can always reassemble the smaller pieces into something new or different.
But what kinds of things do we compose? In Functional Programming, we compose types (called
algebraic data types) and functions.
Composing Types Algebraically
Algebraic Data Types (ADTs) use Algebra to define the total number of values a given type (i.e. named Set) can have.
There are two videos worth watching in this regard. The table and visualizations that follow merely summarize their points, except for the ideas behind the
Tree types in the second video.
- 'Algebraic Data Types' as "Composable Data Types" (stop at 29:26)
- Same ideas already explained in the above "Power of Composition" video:
- It uses a different syntax than
PureScriptbut the ideas still apply.
- The Algebra of Algebraic Data Types
- Warning: video has terrible sound quality!
- explains the "algebraic laws" behind ADTs
Trees (unlike first video)
|Name||Math Operator||Logic Operator||PureScript Type||Idea|
|Product Type||AND||"One value from type |
|Sum Type||OR||"One value from type |
Similar to types, functions also compose but in a slightly different way. Look over the below image and then watch the video at the end (if you haven't seen it already).
Video link: Logging a function's name each time it is called: migrating an "object-oriented paradigm" solution to an "functional paradigm" solution