export class Maybe {

    constructor (f) {
        this._run = f;
    }

    /**
     * Transform the result of a maybe operation.
     */
    map (f) {
        return new this.constructor(t => {
            return Promise.resolve(this._run(t)).then(f).fail(undef)
        });
    }

    /**
     * Extend the current maybe operation by merging it in sequence
     * with another maybe operation returned by f.
     */
    chain (f) {
        return new this.constructor(t => {
            return Promise.resolve(this._run(t))
                .then(x => f(x)._run(t))
                .fail(undef);
        });
    }

    run (t) {
        // what to do if this operation fails????
        return this._run(t);
    }

}

function undef () {}