test1/src/prelude/maybe.ts
Aya Morisawa 96bc17aa10 Check config on load (#4170)
Co-authored-by: syuilo <syuilotan@yahoo.co.jp>
2019-02-06 22:44:55 +09:00

31 lines
606 B
TypeScript

export interface Maybe<T> {
isJust(): this is Just<T>;
map<S>(f: (x: T) => S): Maybe<S>;
getOrElse(x: T): T;
}
export type Just<T> = Maybe<T> & {
get(): T
};
export function just<T>(value: T): Just<T> {
return {
isJust: () => true,
getOrElse: (_: T) => value,
map: <S>(f: (x: T) => S) => just(f(value)),
get: () => value
};
}
export function nothing<T>(): Maybe<T> {
return {
isJust: () => false,
getOrElse: (value: T) => value,
map: <S>(_: (x: T) => S) => nothing<S>()
};
}
export function fromNullable<T>(value: T): Maybe<T> {
return value == null ? nothing() : just(value);
}