tryCatch
import { tryCatch } from "https://raw.githubusercontent.com/baetheus/fun/main/option.ts";
Take a function that can throw and wrap it in a try/catch block. Returns a new function that takes the same arguments as the original but returns the original value wrapped in an Option. If the function throws then the new function returns None, otherwise it returns Some.
@example
import * as O from "./option.ts";
function thrower(n: number): number {
if (n < 0) {
throw new Error("This number is too small");
}
return n;
}
const handler = O.tryCatch(thrower);
const result1 = handler(-1); // None
const result2 = handler(0); // Some(0);