A monad wraps a value and lets you chain operations without worrying about null, undefined, or errors. The Maybe monad handles nullable values. The Either monad handles success/failure paths. Both eliminate defensive null checks and try/catch litter.
Prerequisites: FP #1 — compose/pipe
1. The Problem — Defensive Null Checks Everywhere
Loading editor...
2. Maybe Monad — Chain Without Null Checks
Maybe wraps a value that might be null/undefined. Operations are applied only if the value exists:
Loading editor...
3. Either Monad — Success/Failure Paths
Either has two variants: Right (success) and Left (failure). map only runs on Right. chain enables branching:
Loading editor...
4. Railway-Oriented Programming Visualized
Loading editor...
5. Chaining Async Operations with Either
Loading editor...
Key Takeaways
- Maybe: wraps nullable values —
map/chainskip when null/undefined. Use for deep property access. - Either/Result: wraps operations that may fail —
Rightfor success,Leftfor failure. .map(fn): transforms the wrapped value (if it exists/is success)..chain(fn): likemapbutfnreturns a new monad — enables branching..fold(errFn, okFn): unwrap both paths at the end — the only place errors are handled.- Monads compose — chain
MaybewithEitherfor null-safe, error-safe pipelines.
Next: FP #4 — Point-Free Style & Referential Transparency — write functions without naming arguments.
