Home Reference Source Repository

src/monad.js

/**
 * Class Monad - Base abstract class.
 * @implements {M}
 * @abstract
 */
export class Monad {
    /**
     * Binds transformation function and underlying value to the monad.
     * @method bind
     * @param {MF<T, U> | D<T>} f - transformation function.
     * @param v - underlying value.
     * @return {Promise<U> | Pr<U> | Error | boolean  | void}
     * @abstract
     */
    abstract bind(f, v)
    ;
    /**
     * Takes Error or string return Error.
     * @method fail
     * @param {Error | string} e - Error obj. or string.
     * @return {Error}
     * @protected
     */
    fail(e) {
        return e instanceof Error ? e : new Error(e);
    }
    /**
     * Produces result after execution f(v).
     * @method just
     * @param {function(v: T) => Pr<U>} f - transformation function for a monad.
     * @param {T} v - underlying value.
     * @return {Pr<U>} extracts transformed value by f(v).
     * @protected
     */
    just(f, v) {
        return f(v);
    }
}
//Copyright (c) 2017 Alex Tranchenko. All rights reserved.