import { Failure } from './failure' ; import { Option } from '../option/option' ; import { Success } from './success' ; /**
 * A Try represents a computation that may or may not have succeeded.
 *
 * :TODO: Narrow self-type in `getOrElse` and `orElse` interfaces.
 */ interface Try < A > { isFailure : boolean ; isSuccess : boolean ; equals < B > ( other ) : boolean ; filter ( predicate ) : Try < A > ; flatMap < B > ( f ) : Try < B > ; foreach ( run ) : void ; get ( ) : A ; getOrElse < B > ( defaultValue ) : B ; map < B > ( f ) : Try < B > ; match < B > ( matcher ) : B ; orElse < B > ( alternative ) : Try < B > ; recover ( fn ) : Try < A > ; recoverWith ( fn ) : Try < A > ; toOption ( ) : Option < A > ; } function Try < A > ( fn ) { try { return new Success ( fn ( ) ) ; } catch ( e ) { return new Failure < A > ( e as Error ) ; } } namespace Try { export function flatten < A > ( tries ) { return tries . filter ( ( attempt ) : boolean => attempt . isSuccess ) . map ( ( attempt ) : A => attempt . get ( ) ) ; } } export { Try } ;