"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectAllWith = exports.collectAllWith_ = exports.collectAllUnit = exports.collectAll = exports.forEachUnit = exports.forEachUnit_ = exports.forEach = exports.forEach_ = void 0;
// ets_tracing: off
const ChunkFilterMap = __importStar(require("../Collections/Immutable/Chunk/api/filterMap"));
const Chunk = __importStar(require("../Collections/Immutable/Chunk/core"));
const Function_1 = require("../Function");
const core = __importStar(require("./core"));
/**
 * Applies the function `f` to each element of the `Iterable<A>` and
 * returns the results in a new `readonly B[]`.
 *
 * For a parallel version of this method, see `forEachPar`.
 * If you do not need the results, see `forEachUnit` for a more efficient implementation.
 */
function forEach_(as, f) {
    return core.suspend(() => {
        const acc = [];
        return core.map_(forEachUnit_(as, (a) => core.map_(f(a), (b) => {
            acc.push(b);
        })), () => Chunk.from(acc));
    });
}
exports.forEach_ = forEach_;
/**
 * Applies the function `f` to each element of the `Iterable<A>` and
 * returns the results in a new `readonly B[]`.
 *
 * For a parallel version of this method, see `forEachPar`.
 * If you do not need the results, see `forEachUnit` for a more efficient implementation.
 *
 * @ets_data_first forEach_
 */
function forEach(f) {
    return (as) => forEach_(as, f);
}
exports.forEach = forEach;
function forEachUnitLoop(iterator, f) {
    const next = iterator.next();
    return next.done
        ? core.unit
        : core.chain_(f(next.value), () => forEachUnitLoop(iterator, f));
}
/**
 * Applies the function `f` to each element of the `Iterable<A>` and runs
 * produced Syncs sequentially.
 *
 * Equivalent to `asUnit(forEach(as, f))`, but without the cost of building
 * the list of results.
 */
function forEachUnit_(as, f, __trace) {
    return core.suspend(() => forEachUnitLoop(as[Symbol.iterator](), f));
}
exports.forEachUnit_ = forEachUnit_;
/**
 * Applies the function `f` to each element of the `Iterable<A>` and runs
 * produced Syncs sequentially.
 *
 * Equivalent to `asUnit(forEach(as, f))`, but without the cost of building
 * the list of results.
 *
 * @ets_data_first forEachUnit_
 */
function forEachUnit(f, __trace) {
    return (as) => forEachUnit_(as, f);
}
exports.forEachUnit = forEachUnit;
/**
 * Evaluate each Sync in the structure from left to right, and collect the
 * results. For a parallel version, see `collectAllPar`.
 */
function collectAll(as) {
    return forEach_(as, Function_1.identity);
}
exports.collectAll = collectAll;
/**
 * Evaluate each Sync in the structure from left to right, and discard the
 * results. For a parallel version, see `collectAllUnitPar`.
 */
function collectAllUnit(as, __trace) {
    return forEachUnit_(as, Function_1.identity);
}
exports.collectAllUnit = collectAllUnit;
/**
 * Evaluate each Sync in the structure with `collectAll`, and collect
 * the results with given partial function.
 */
function collectAllWith_(as, pf, __trace) {
    return core.map_(collectAll(as), ChunkFilterMap.filterMap(pf));
}
exports.collectAllWith_ = collectAllWith_;
/**
 * Evaluate each Sync in the structure with `collectAll`, and collect
 * the results with given partial function.
 *
 * @ets_data_first collectAllWith_
 */
function collectAllWith(pf) {
    return (as) => collectAllWith_(as, pf);
}
exports.collectAllWith = collectAllWith;
//# sourceMappingURL=excl-forEach.js.map