Functional Programming

Type-level Collatz Sequence

Implementing the famous Collatz conjecture entirely within TypeScript’s type system, demonstrating type-level arithmetic and recursive computation. The sequence follows $n \rightarrow n/2$ if even, $n \rightarrow 3n+1$ if odd, conjecturing all positive integers reach 1. Using template literal types and conditional type recursion, we can compute Collatz<27> yielding the 111-step sequence purely at compile time - pushing TypeScript’s type system to perform actual mathematical computation.

Typesafe Function Composition

This deep dive explores implementing mathematically-correct function composition with full type safety. We’ll build a compose function where compose<A, B, C>(f: B→C, g: A→B): A→C validates type compatibility at compile time, handle variadic arguments through recursive types, and ensure composed pipelines like compose(toString, Math.sqrt, parseInt) automatically infer as (s: string) => string while preventing invalid compositions from compiling.