This commit is contained in:
syuilo 2019-02-07 21:02:33 +09:00
parent 27768081e2
commit 5448c22031
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
19 changed files with 125 additions and 194 deletions

View file

@ -1,7 +1,5 @@
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> & {
@ -11,8 +9,6 @@ export type Just<T> = Maybe<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
};
}
@ -20,11 +16,5 @@ export function just<T>(value: T): Just<T> {
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);
}