maybetyped
Version:
Well-typed functional Maybe monad
30 lines (29 loc) • 1.31 kB
TypeScript
import { Nullable } from 'simplytyped';
import * as fl from './fantasy-land';
export declare const binder: <T extends Function>(context: any, f: T) => T;
export declare type Nil = null | undefined;
export interface MatchType<T, R> {
some?: (v: T) => R;
none?: () => R;
}
export declare const isNothing: (thing: any) => thing is Nil;
export default abstract class Maybe<T> {
protected value: Nullable<T>;
protected constructor(value: Nullable<T>);
static of: <T>(x: T) => Maybe<T>;
isNothing(): boolean;
abstract expect(msg?: string | Error): T;
abstract caseOf<R>(funcs: MatchType<T, R>): Maybe<R>;
abstract map<U>(f: (v: T) => Nullable<U>): Maybe<U>;
abstract tap(f: (v: T) => void): Maybe<T>;
abstract flatMap<U>(f: (v: T) => Maybe<U>): Maybe<U>;
abstract orElse<U>(def: U | (() => U)): T | U;
abstract or<U>(other: Maybe<U> | (() => Maybe<U>)): Maybe<T | U>;
abstract eq(other: Maybe<T>): boolean;
abstract asNullable(): T | null;
abstract join<U, R>(f: (x: T, y: U) => R | Nil, other: Maybe<U>): Maybe<R>;
static [fl.of]: <T>(x: T) => Maybe<T>;
[fl.map]: <U>(f: (v: T) => Nullable<U>) => Maybe<U>;
[fl.chain]: <U>(f: (v: T) => Maybe<U>) => Maybe<U>;
[fl.ap]: <U>(m: Maybe<(x: T) => U>) => Maybe<U>;
}