Opt<T>

Anatomy

type OptVariants<T> = {
  Some: T;
  None: null;
};

Import

import { Opt, Some, None } from "tnum/std";

Usage

import { Opt, Some, None } from "tnum/std";

// Some(v)
const some: Opt<number> = Some(10);
// None
const none: Opt<number> = None();

// unwrap
some.unwrap(); // returns 10.
none.unwrap(); // throws error.

Methods

Method callSome(1024)NoneDescription
clone()Some(1024) - clonedNoneReturns clone.
isSome()true - narrowed to Some(number)falseReturns true if the varaint is Some.
isSomeAnd((v) => v > 2048)falsefalseIf the variant is Some, returns the result of the passed function. Otherwise, returns false.
isNone()falsetrue - narrowed to NoneReturns true if the variant is None
unwrap()1024throws errorIf the variant is Some(v), returns v. Otherwise throws an error.
unwrapOr(0)10240If the variant is Some(v), returns v. Otherwise returns passed argument value.
unwrapOrElse(() => 0)10240If the variant is Some(v), returns v. Otherwise returns the result the passed function.
map((v) => v * 2)Some(2048)NoneTODO
mapOr(1024, (v) => v * 2)20481024TODO
mapOrElse(() => 1024, (v) => v * 2)20481024TODO
and(Some(2048))Some(2048) - clonedNoneTODO
and(None)NoneNoneTODO
andThen((v) => Some(v * 2))Some(2048)NoneTODO
andThen((v) => None)NoneNoneTODO
contains(1024)truefalseTODO
filter((v) => v === 1024)Some(1024) - clonedNoneTODO
filter((v) => v === 512)NoneNoneTODO
flatten()Some(1024)NoneTODO
inspect((v) => console.log(v))outputs 1024do nothingTODO
or(Some(512))Some(1024) - clonedSome(512) - clonedTODO
or(None)Some(1024) - clonedNoneTODO
orElse(() => Some(512))Some(1024) - clonedSome(512) - clonedTODO
orElse(() => None)Some(1024) - clonedNoneTODO
xor(Some(512))NoneSome(512) - clonedTODO
xor(None)Some(1024) - clonedNoneTODO
zip(Some(512))Some([1024, 512])NoneTODO
zipWith(Some(512), (a, b) => {a, b})Some({a: 1024, b: 512})NoneTODO
okOr("none...")Ok(1024)Err("none...")TODO
okOrElse(() => "none...")Ok(1024)Err("none...")TODO