Skip to content

Instantly share code, notes, and snippets.

@VincentCordobes
Last active March 5, 2018 18:42
Show Gist options
  • Save VincentCordobes/e62d7e9f82195348d18c157eb25e2c9a to your computer and use it in GitHub Desktop.
Save VincentCordobes/e62d7e9f82195348d18c157eb25e2c9a to your computer and use it in GitHub Desktop.
export type Result<T, E> = Ok<T, E> | Err<T, E>
class Ok<T, E> {
value: T;
constructor(v: T) {
this.value = v
}
bind<U>(f: (v: T) => Result<U, E>): Result<U, E> {
return f(this.value);
}
map<U>(f: (v: T) => U): Result<U, E> {
return succeed(f(this.value));
}
mapErr<U>(f: (e: E) => U): Result<T, U> {
return succeed(this.value);
}
unwrapOrThrow(): T {
return this.value;
}
}
class Err<T, E> {
error: E;
constructor(e: E) {
this.error = e
}
bind<U>(f: (v: T) => Result<U, E>): Result<U, E> {
return fail(this.error);
}
map<U>(f: (v: T) => U): Result<U, E> {
return fail(this.error);
}
mapErr<U>(f: (e: E) => U): Result<T, U> {
return fail(f(this.error));
}
unwrapOrThrow(): T {
throw new Error('Cannot unwrap None value')
}
}
export function isOk<T, E>(r: Result<T, E>): r is Ok<T, E> {
return r instanceof Ok;
}
export function isError<T, E>(r: Result<T, E>): r is Err<T, E> {
return r instanceof Err;
}
export function succeed<T, E>(value: T): Result<T, E> {
return new Ok(value);
}
export function fail<T, E>(error: E): Result<T, E> {
return new Err(error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment