Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
chain-able-deps / dist / loop / fantasy / _reduce.js
Size: Mime:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var is_1 = require("../../is");
var bind_1 = require("../../fp/bind");
var ON_INIT = '@@transducer/init';
var ON_STEP = '@@transducer/step';
var ON_REDUCED = '@@transducer/reduced';
var ON_VALUE = '@@transducer/value';
var ON_RESULT = '@@transducer/result';
var FANTASY_LAND_REDUCE = 'fantasy-land/reduce';
// const _xwrap = (() => {
//   function XWrap(fn) {
//     this.f = fn
//   }
//   XWrap.prototype[ON_INIT] = () => {
//     throw new Error('init not implemented on XWrap')
//   }
//   XWrap.prototype[ON_RESULT] = acc => acc
//   XWrap.prototype[ON_STEP] = function(acc, x) {
//     return this.f(acc, x)
//   }
//   // @TODO construct?
//   return function _xwrap(fn) {
//     return new XWrap(fn)
//   }
// })()
var XWrapClass = /** @class */ (function () {
    function XWrapClass(fn) {
        this.f = fn;
    }
    XWrapClass.prototype[ON_INIT] = function () {
        throw new Error('init not implemented on XWrap');
    };
    XWrapClass.prototype[ON_RESULT] = function (acc) {
        return acc;
    };
    XWrapClass.prototype[ON_STEP] = function (acc, x) {
        return this.f(acc, x);
    };
    return XWrapClass;
}());
// @TODO construct?
function _xwrap(fn) {
    return new XWrapClass(fn);
}
/**
 * @name _reduce
 * @since 5.0.0-beta.6
 * @memberOf loop
 * @return {Function}
 */
// list aka functor
var reduce = (function () {
    function _arrayReduce(xf, acc, list) {
        var idx = 0;
        var len = list.length;
        while (idx < len) {
            acc = xf[ON_STEP](acc, list[idx]);
            if (acc && acc[ON_REDUCED]) {
                acc = acc[ON_VALUE];
                break;
            }
            idx += 1;
        }
        return xf[ON_RESULT](acc);
    }
    function _iterableReduce(xf, acc, iter) {
        var step = iter.next();
        while (!step.done) {
            acc = xf[ON_STEP](acc, step.value);
            if (is_1.hasOwnProperty(acc, ON_REDUCED)) {
                acc = acc[ON_VALUE];
                break;
            }
            step = iter.next();
        }
        return xf[ON_RESULT](acc);
    }
    function _methodReduce(xf, acc, obj, methodName) {
        return xf[ON_RESULT](obj[methodName](bind_1.default(xf[ON_STEP], xf), acc));
    }
    return function _reduce(fn, acc, list) {
        if (is_1.isFunction(fn)) {
            fn = _xwrap(fn);
        }
        if (is_1.isArrayLike(list)) {
            return _arrayReduce(fn, acc, list);
        }
        else if (is_1.isFunction(list[FANTASY_LAND_REDUCE])) {
            return _methodReduce(fn, acc, list, FANTASY_LAND_REDUCE);
        }
        else if (!is_1.isNil(list[Symbol.iterator])) {
            return _iterableReduce(fn, acc, list[Symbol.iterator]());
        }
        else if (is_1.isFunction(list.next)) {
            return _iterableReduce(fn, acc, list);
        }
        else if (is_1.isFunction(list.reduce)) {
            return _methodReduce(fn, acc, list, 'reduce');
        }
        throw new TypeError('reduce: list must be array or iterable');
    };
})();
exports.default = reduce;
//# sourceMappingURL=_reduce.js.map