Functional Programming to the max
A small number guessing game, rewritten step by step from procedural code into a functional program with its own IO type. The aim is not purity for its own sake, but more principled code.
From closures to lambda calculus to ZIO.
8 posts
A small number guessing game, rewritten step by step from procedural code into a functional program with its own IO type. The aim is not purity for its own sake, but more principled code.
Wile E. Coyote wants a purchase summary for every ACME customer. The same program is built imperatively, then with monad transformers, then with ZIO, which keeps it pure without paying for it at runtime.
Pierre the tightrope walker and his birds, simulated with exceptions, nulls and finally an Option. Functional programming turns out to be more than a refactor: a computation context gives you honest types and safe sequencing.
Lambda calculus is usually shown off with numbers and recursion. Here it goes to work on an everyday CMS problem instead: finding every URL for the sitemap, and each page's alternate language URLs.
A set library where each set is nothing but a function that says yes or no to an element. Union, intersection and difference follow naturally, and the line between code and data starts to blur.
Filter, map and reduce, put to work on a dataset of house listings. Four-bedroom houses in Cambria or the average listing price take a few composed functions, and maybe turn up a dream home.
Summing integers, cubes and factorials starts as three near-identical functions. A higher order function folds them into one, showing how JavaScript's first-class functions simplify everyday code.
SICP's picture language, translated from Scheme to JavaScript, where painters are just functions. Combining them with closures builds intricate patterns from simple parts, and shows why stratified design keeps programs easy to change.