The purpose of this substack is to introduce those at varying points on their adventure through the world of functional programming. There is a lot of material on functional programming in the small (e.g. tutorials on lodash in JS to functions on lists in Haskell) and a wealth of articles and papers on more advanced techniques in functional programming but not a lot of practical bridging the gap for industry developers. This substack is a small contribution toward that goal.
Over the weekend I tweeted about the top four big ideas that fundamentally changed the way I write software today.
One of the big ideas listed was algebraic data types (ADTs) which I learned about shortly after picking up Haskell my first time around. This post will be a small tribute to them showing that algebraic data types are taking the even mainstream programming languages by storm.
I Have No Idea What You Are Talking About, Susan!
No problem. Here are some links you might find useful along the way:
OCaml’s documentation introduces records (products) and variants (sum types)
This blog post from 2019 introduces some key algebraic data types.
In 2012, I wrote a blog post with Scala2 and Haskell examples about the idea that illustrates the basic constructions.
For The Math Inclined
Moved content to my own site: Algebraic Data Types for the Math(s) Inclined
Algebraic Data Types in TypeScript
A growing number of JavaScript developers are moving toward TypeScript, a fast-growing mainstream programming language with some capabilities of writing software using functional techniques.
I wrote a blog post inspired by a work discussion and live coding session last week with coworkers about encoding algebraic data types in TypeScript (the language their codebase is written).
PEP-Py Algebraic Data Types
A Python PEP was first proposed just over a month ago titled, ‘Structural Pattern Matching’ (PEP 622) which contains a section called ‘Sealed classes as algebraic data types’. This is the first official introduction to ADTs into Python at the language and standard library level. Other attempts have been made as a user-contributed library called (unsurprisingly) algebraic-data-types.
Mobile Developers Can Use Them Too!
In Kotlin you can use sealed classes to give you an encoding of algebraic data types not unlike how you would encode algebraic data types in Scala2.
Swift developers can use Enumerations to encode algebraic data types in your iOS codebases and since a switch statement must be exhaustive when considering an enumeration’s cases you can get exhaustivity checks.
The world seems finally ready for algebraic data types and I am really excited about it. Even Ruby announced in its last major release (version 2.7) that it was getting pattern matching. A more formal encoding of algebraic data types is not far off, especially with types coming in Ruby 3.0. :)
It’s a very simple idea that promotes compositional data type definitions which you can use to describe very complex data structures. If your language offers builtin support you get [static] exhaustivity checking and typically pattern matching which are both great to have, saving the developer time to model their application domain better. That’s the basic big idea yet I made you read all the above so that hopefully, you get excited too.
Cheers! :)
PS Unrelated to algebraic data types but related to functional programming in production: I wrote a high-level review of building a PureScript serverless application after launching it to production and looking at our first week of production metrics. You can read my overview here and peruse the barebones PureScript (Express) starter repository on GitHub. I plan to provide teaching material introducing the code later.