Home Reference Source Repository

src/maybe.js

import { Monad } from "./monad";
/**
 * Class Maybe - return given value or produce null if take nothing or get nothing after execution of f(v).
 * @extends {Monad}
 */
export class Maybe extends Monad {
    /**
     * Chains the operations on a monadic values.
     * @method bind
     * @param {MF<T, U>} f - transformation function for a monad.
     * @param {T} v - underlying value for a monad.
     * @return {Pr<U>} transformed by f() value v.
     */
    bind(f, v) {
        if (v === null || v === undefined) {
            return this.nothing();
        }
        else {
            const vL = this.just(f, v);
            return (vL === null || vL === undefined) ? this.nothing() : vL;
        }
    }
    /**
     * Return nothing (null).
     * @method nothing
     * @return {null}
     */
    nothing() {
        return null;
    }
    ;
}
//Copyright (c) 2017 Alex Tranchenko. All rights reserved.