export interface Lazy < T > { readonly value : T ; readonly hasValue : boolean ; map < R > ( f ) : Lazy < R > ; } class LazyValue < T > implements Lazy < T > { private _hasValue : boolean = false ; private _value ? : T ; constructor ( private readonly _getValue ) { } get value ( ) : T { if ( ! this . _hasValue ) { this . _hasValue = true ; this . _value = this . _getValue ( ) ; } return this . _value ! ; } get hasValue ( ) : boolean { return this . _hasValue ; } public map < R > ( f ) { return new LazyValue ( ( ) => f ( this . value ) ) ; } } export function lazy < T > ( getValue ) { return new LazyValue < T > ( getValue ) ; }