fn()
if resA
is an Ok
.
import { andThen, err, ok, Result } from "./result.ts";
import { assertEquals } from "../deps.ts";
const sqrtThenToString = andThen(
(num: number): Result<string, string> =>
num < 0
? err("num must not be negative")
: ok(Math.sqrt(num).toString()),
);
assertEquals(sqrtThenToString(ok(4)), ok("2"));
assertEquals(sqrtThenToString(ok(-1)), err("num must not be negative"));
assertEquals(sqrtThenToString(err("not a number")), err("not a number"));
Generated using TypeDoc
Returns
fn(v)
ifresA
is anOk(v)
, otherwise returns the errorresA
. This is an implementation ofFlatMap
. The order of arguments is reversed because of that it is useful for partial applying.