Result
Overview
type Result<T, E> = Ok<T> | Err<E>
See also the Result
Methods
and
and<U, T, E>(this: Result<T, E>, res: Result<U, E>): Result<U, E>
Returns res
if this
is Ok
, otherwise returns Err
.
import { err, ok } from "perlica/Result";
const a = ok(4);
const b = err("new error");
expect(a.and(b)).toEqual(err("new error"));
const a = err("old error");
const b = ok(4);
expect(a.and(b)).toEqual(err("old error"));
const a = err("old error");
const b = err("new error");
expect(a.and(b)).toEqual(err("old error"));
const a = ok(4);
const b = ok(12);
expect(a.and(b)).toEqual(ok(12));
See also the Result.and
andThen
andThen<U, T, E>(this: Result<T, E>, f: (v: T) => Result<U, E>): Result<U, E>
Returns f
call if this
is Ok
, otherwise returns Err
.
import { err, ok } from "perlica/Result";
const string_to_number = (num: string): Result<number, Error> => {
const v = Number(num);
return isNaN(v) ? err(Error("Not a Number")) : ok(v);
};
expect(ok("4").andThen(string_to_number)).toEqual(ok(4));
expect(ok("hello").andThen(string_to_number)).toEqual(err(Error("Not a Number")));
expect(err(Error("old error")).andThen(string_to_number)).toEqual(err(Error("old error")));
See also the Result.and_then
expect
expect<T, E>(this: Result<T, E>, msg: string): T
Returns the contained value T
if this
is Ok
, otherwise throw exception value E
.
import { err, ok } from "perlica/Result";
const a = ok(4);
const b = err("error");
expect(a.expect("my error message")).toEqual(4);
expect(() => b.expect("my error message")).toThrowError("my error message");
See also the Result.expect
expectErr
expectErr<T, E>(this: Result<T, E>, msg: string): E
Returns the contained value E
if this
is Err
, otherwise throw exception msg
.
import { err, ok } from "perlica/Result";
const a = ok(4);
const b = err("error");
expect(() => a.expectErr("my error message")).toThrowError("my error message");
expect(t).toEqual("error");
See also the Result.expect_err
unwrap
unwrap<T, E>(this: Result<T, E>): T
Returns the contained value Ok
, otherwise throw Error
exception.
import { err, ok } from "perlica/Result";
expect(ok(4).unwrap()).toEqual(4);
expect(() => err("error").unwrap()).toThrowError(`called \`Result.unwrap()\` on an \`Err\` value: error`);
See also the Result.unwrap
unwrapErr
unwrapErr<T, E>(this: Result<T, E>): E
Returns the contained value Err
, otherwise throw Error
exception.
import { err, ok } from "perlica/Result";
expect(() => ok(4).unwrapErr()).toThrowError(`called \`Result.unwrapErr()\` on an \`Ok\` value: 4`);
expect(err("error").unwrapErr()).toEqual("error");
See also the Result.unwrap_err
unwrapOr
unwrapOr<T, E>(this: Result<T, E>, def: T): T
Returns the contained value Ok
, otherwise returns def
.
import { err, ok } from "perlica/Result";
expect(ok(4).unwrapOr(10)).toEqual(4);
expect(err("error").unwrapOr(10)).toEqual(10);
See also the Result.unwrap_or
unwrapOrElse
unwrapOrElse<T, E>(this: Result<T, E>, def: (v: E) => T): T
Returns the contained value Ok
, otherwise def
call.
import { err, ok } from "perlica/Result";
const len = (s: string) => s.length;
expect(ok(4).unwrapOrElse(len)).toEqual(4);
expect(err("error").unwrapOrElse(len)).toEqual(5);
See also the Result.unwrap_or_else
Functions
and
export const and = <U, T, E>(res1: Result<T, E>, res2: Result<U, E>): Result<U, E>
Returns res2
if res1
is Ok
, otherwise returns res1
.
import * as R from "perlica/Result";
const a = R.ok(4);
const b = R.err("new error");
expect(R.and(a, b)).toEqual(R.err("new error"));
const a = R.err("old error");
const b = R.ok(4);
expect(R.and(a, b)).toEqual(R.err("old error"));
const a = R.err("old error");
const b = R.err("new error");
expect(R.and(a, b)).toEqual(R.err("old error"));
const a = R.ok(4);
const b = R.ok(12);
expect(R.and(a, b)).toEqual(R.ok(12));
See also the Result.and
andThen
export const andThen = <U, T, E>(res1: Result<T, E>, f: (v: T) => Result<U, E>): Result<U, E>
Returns f
call if res1
is Ok
, otherwise returns res1
.
import * as R from "perlica/Result";
const string_to_number = (num: string): R.Result<number, Error> => {
const v = Number(num);
return isNaN(v) ? R.err(Error("Not a Number")) : R.ok(v);
};
expect(R.andThen(R.ok("4"), string_to_number)).toEqual(R.ok(4));
expect(R.andThen(R.ok("hello"), string_to_number)).toEqual(R.err(Error("Not a Number")));
expect(R.andThen(R.err(Error("old error")), string_to_number)).toEqual(R.err(Error("old error")));
See also the Result.and_then
expect
export const expect = <T, E>(res: Result<T, E>, msg: string): T
Returns the contained value T
if res
is Ok
, otherwise throw exception msg
.
import * as R from "perlica/Result";
const a = R.ok(4);
const b = R.err("error");
expect(R.expect(a, "my error message")).toEqual(4);
expect(() => R.expect(b, "my error message")).toThrowError("my error message");
See also the Result.expect
expectErr
export const expectErr = <T, E>(res: Result<T, E>, msg: string): E
Returns the contained value E
if res
is Err
, otherwise throw exception msg
.
import * as R from "perlica/Result";
const a = R.ok(4);
const b = R.err("error");
expect(() => R.expectErr(a, "my error message")).toThrowError("my error message");
expect(R.expectErr(b, "my error message")).toEqual("error");
See also the Result.expect_err
unwrap
export const unwrap = <T, E>(self: Result<T, E>): T
Returns the contained value Ok
, otherwise throw Error
exception.
import * as R from "perlica/Result";
expect(R.unwrap(R.ok(4))).toEqual(4);
expect(() => R.unwrap(R.err("error"))).toThrowError(`called \`Result.unwrap()\` on an \`Err\` value: error`);
See also the Result.unwrap
unwrapErr
export const unwrapErr = <T, E>(self: Result<T, E>): E
Returns the contained value Err
, otherwise throw Error
exception.
import * as R from "perlica/Result";
expect(() => R.unwrapErr(R.ok(4))).toThrowError(`called \`Result.unwrapErr()\` on an \`Ok\` value: 4`);
expect(R.unwrapErr(R.err("error"))).toEqual("error");
See also the Result.unwrap_err
unwrapOr
export const unwrapOr = <T, E>(self: Result<T, E>, def: T): T
Returns the contained value Ok
, otherwise returns def
.
import * as R from "perlica/Result";
expect(R.unwrapOr(R.ok(4), 10)).toEqual(4);
expect(R.unwrapOr(R.err("error"), 10)).toEqual(10);
See also the Result.unwrap_or
unwrapOrElse
export const unwrapOrElse = <T, E>(self: Result<T, E>, def: (v: E) => T): T
Returns the contained value Ok
, otherwise def
call.
import * as R from "perlica/Result";
const len = (s: string) => s.length;
expect(R.unwrapOrElse(R.ok(4), len)).toEqual(4);
expect(R.unwrapOrElse(R.err("error"), len)).toEqual(5);
See also the Result.unwrap_or_else