OCaml Examples
OCaml aligned plain-text table renderer
Measure column widths and print a pipe-separated table with right-padded cells that aligns in any terminal.
OCaml bounded worker pool using Domain.spawn
Run independent tasks on a fixed number of OCaml 5 domains, collecting results and exceptions in one list.
OCaml base64 encoder and validating decoder
Encode a string as standard Base64 and decode it back, validating length, padding and alphabet characters.
OCaml input validation collecting field errors
Validate every field of an input record at once and return field names paired with human-readable messages.
OCaml flat JSON decoder with entry-index errors
Decode a flat JSON object into key/value pairs while reporting the failing entry index and offending text.
OCaml percent-encode and split URL query strings
Percent-encode key/value pairs into a query string and split an incoming query string back into its pairs.
OCaml GET JSON through curl with a hard timeout
Fetch JSON with curl and a hard timeout, then extract a named string field with clear error reporting.
OCaml environment config with typed validation
Read required and optional environment variables, parse integers, and return precise validation errors.
OCaml token bucket rate limiter over Unix time
Allow short bursts up to a bucket size while enforcing an average request rate, using monotonic refill math.
OCaml bounded LRU cache on an association list
Keep the most recently used keys in a fixed-capacity association-list cache that drops the least recent entry.
OCaml stable multi-key sort with nulls last
Sort records by city, then optional age with missing values last, then name, using a stable multi-key comparator.
OCaml split a list into fixed-size batches
Split a list into consecutive batches of at most n items, preserving order and rejecting non-positive sizes.
OCaml line-by-line file reader that closes its channel
Walk a text file line by line and collect its lines, closing the channel safely even if the callback raises.
OCaml deep clone and recursive merge for config trees
Copy a nested tree of key/value nodes and deep-merge a patch into it, keeping the original untouched.
OCaml retry loop with capped exponential backoff
Run a fallible operation until it succeeds, sleeping with jittered exponential backoff capped at a fixed ceiling.