/**
 * Interface for a value that may not be there.
 */ export interface Maybe < T > { isJust ( ) : boolean ; isNothing ( ) : boolean ; get ( ) : T ; map < R > ( f ) : Maybe < R > ; flatMap < R > ( f ) : Maybe < R > ; orElse < R > ( f ) : Maybe < T | R > ; } /**
 * Class for a singleton object representing all values that are not there.
 */ export class Nothing < T > implements Maybe < T > { isJust ( ) { return false ; } isNothing ( ) { return true ; } get ( ) { throw new Error ( 'Cannot invoke get() on Nothing' ) ; } map < R > ( _f ) { return this as any as Nothing < R > ; } flatMap < R > ( _f ) { return this as any as Nothing < R > ; } orElse < R > ( f ) { return f ( ) ; } } /**
 * A value in the Maybe type.
 */ export class Just < T > implements Maybe < T > { constructor ( readonly value ) { } isJust ( ) { return true ; } isNothing ( ) { return false ; } get ( ) { return this . value ; } map < R > ( f ) { return new Just ( f ( this . get ( ) ) ) ; } flatMap < R > ( f ) { return f ( this . get ( ) ) ; } orElse < R > ( _f ) { return this ; } } /**
 * Singleton nothing value.
 */ export const nothing = new Nothing ( ) ; /**
 * Shorthand to create a Just<T>.
 */ export const just = < T > ( value ) : Maybe < T > => new Just ( value ) ;