"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dropUntil = exports.dropWhileFn = exports.asyncDropWhile = exports.dropWhile = exports.asyncTakeUntilFn = exports.takeUntilFn = exports.asyncTakeUntil = exports.takeUntil = exports.asyncTakeWhileFn = exports.takeWhileFn = exports.asyncTakeWhile = exports.takeWhile = exports.asyncDropFn = exports.dropFn = exports.asyncDrop = exports.drop = exports.asyncTakeFn = exports.takeFn = exports.asyncTake = exports.take = exports.slice = exports.asyncNotEmpty = exports.notEmpty = exports.asyncEmpty = exports.empty = exports.asyncOnly = exports.only = exports.asyncLast = exports.last = exports.asyncInitial = exports.initial = exports.asyncUnshiftFn = exports.unshiftFn = exports.asyncUnshift = exports.unshift = exports.asyncPushFn = exports.pushFn = exports.asyncPush = exports.push = exports.asyncTail = exports.tail = exports.asyncFirst = exports.first = exports.asyncToSet = exports.toSet = exports.asyncToArray = exports.toArray = exports.isAsyncIterable = exports.asyncIterator = exports.asyncIterable = void 0;
exports.index = exports.asyncFold1Fn = exports.fold1Fn = exports.asyncFold1 = exports.fold1 = exports.asyncFoldFn = exports.foldFn = exports.asyncFold = exports.fold = exports.asyncRemoveFirstFn = exports.removeFirstFn = exports.asyncRemoveFirst = exports.removeFirst = exports.asyncRemoveFn = exports.removeFn = exports.asyncRemove = exports.remove = exports.asyncExcludeFirstFn = exports.excludeFirstFn = exports.asyncExcludeFirst = exports.excludeFirst = exports.asyncExcludeNull = exports.excludeNull = exports.asyncExcludeFn = exports.excludeFn = exports.asyncExclude = exports.exclude = exports.asyncFilterFn = exports.filterFn = exports.asyncFilter = exports.filter = exports.asyncMapFn = exports.mapFn = exports.asyncMap = exports.map = exports.asyncPrefixMatchFn = exports.prefixMatchFn = exports.asyncPrefixMatch = exports.prefixMatch = exports.asyncNotEqualFn = exports.notEqualFn = exports.asyncNotEqual = exports.notEqual = exports.asyncEqualFn = exports.equalFn = exports.asyncEqual = exports.equal = exports.asyncDropUntilFn = exports.dropUntilFn = exports.asyncDropUntil = void 0;
exports.concat = exports.asyncAll = exports.all = exports.asyncAny = exports.any = exports.asyncOr = exports.or = exports.asyncAnd = exports.and = exports.asyncAverage = exports.average = exports.asyncProduct = exports.product = exports.asyncSum = exports.sum = exports.asyncMinimumByFn = exports.minimumByFn = exports.asyncMinimumBy = exports.minimumBy = exports.asyncMinimumFn = exports.minimumFn = exports.asyncMinimum = exports.minimum = exports.asyncMaximumByFn = exports.maximumByFn = exports.asyncMaximumBy = exports.maximumBy = exports.asyncMaximumFn = exports.maximumFn = exports.asyncMaximum = exports.maximum = exports.asyncFindFn = exports.findFn = exports.asyncFind = exports.find = exports.asyncFindIndexFn = exports.findIndexFn = exports.asyncFindIndex = exports.findIndex = exports.asyncIndexOfFn = exports.indexOfFn = exports.asyncIndexOf = exports.indexOf = exports.asyncContainsFn = exports.containsFn = exports.asyncContains = exports.contains = exports.asyncIndexFn = exports.indexFn = exports.asyncIndex = void 0;
exports.asyncMapKeyLastByFn = exports.mapKeyLastByFn = exports.asyncMapKeyLastBy = exports.mapKeyLastBy = exports.asyncMapKeyFirstByFn = exports.mapKeyFirstByFn = exports.asyncMapKeyFirstBy = exports.mapKeyFirstBy = exports.asyncMapKeyByFn = exports.mapKeyByFn = exports.asyncMapKeyBy = exports.mapKeyBy = exports.asyncKeyLastByFn = exports.keyLastByFn = exports.asyncKeyLastBy = exports.keyLastBy = exports.asyncKeyFirstByFn = exports.keyFirstByFn = exports.asyncKeyFirstBy = exports.keyFirstBy = exports.asyncKeyByFn = exports.keyByFn = exports.asyncKeyBy = exports.keyBy = exports.asyncZipFn = exports.zipFn = exports.asyncZip = exports.zip = exports.pairwise = exports.scan1Fn = exports.asyncScan1 = exports.scan1 = exports.asyncScanFn = exports.scanFn = exports.asyncScan = exports.scan = exports.asyncNoneNull = exports.noneNull = exports.asyncConcatMapFn = exports.concatMapFn = exports.asyncConcatMap = exports.concatMap = exports.asyncAppend = exports.append = exports.asyncPrepend = exports.prepend = exports.asyncConcat = void 0;
const tslib_1 = require("tslib");
const ordered_1 = require("@softwareventures/ordered");
const nullable_1 = require("@softwareventures/nullable");
function asyncIterable(iterable) {
    return tslib_1.__asyncGenerator(this, arguments, function* asyncIterable_1() {
        var _a, e_1, _b, _c;
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    yield yield tslib_1.__await(element);
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_1) throw e_1.error; }
        }
    });
}
exports.asyncIterable = asyncIterable;
function asyncIterator(iterable) {
    return asyncIterable(iterable)[Symbol.asyncIterator]();
}
exports.asyncIterator = asyncIterator;
function isAsyncIterable(value) {
    return typeof value === "object" && value != null && Symbol.asyncIterator in value;
}
exports.isAsyncIterable = isAsyncIterable;
async function toArray(iterable) {
    var _a, e_2, _b, _c;
    const array = [];
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                array.push(element);
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return array;
}
exports.toArray = toArray;
exports.asyncToArray = toArray;
async function toSet(iterable) {
    var _a, e_3, _b, _c;
    const set = new Set();
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                set.add(element);
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_3_1) { e_3 = { error: e_3_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_3) throw e_3.error; }
    }
    return set;
}
exports.toSet = toSet;
exports.asyncToSet = toSet;
async function first(iterable) {
    var _a, e_4, _b, _c;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                return element;
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_4_1) { e_4 = { error: e_4_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_4) throw e_4.error; }
    }
    return null;
}
exports.first = first;
exports.asyncFirst = first;
function tail(iterable) {
    return tslib_1.__asyncGenerator(this, arguments, function* tail_1() {
        const iterator = asyncIterator(iterable);
        yield tslib_1.__await(iterator.next());
        let element = yield tslib_1.__await(iterator.next());
        while (element.done !== true) {
            yield yield tslib_1.__await(element.value);
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.tail = tail;
exports.asyncTail = tail;
function push(iterable, value) {
    return tslib_1.__asyncGenerator(this, arguments, function* push_1() {
        var _a, e_5, _b, _c;
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    yield yield tslib_1.__await(element);
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_5_1) { e_5 = { error: e_5_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_5) throw e_5.error; }
        }
        yield yield tslib_1.__await(yield tslib_1.__await(value));
    });
}
exports.push = push;
exports.asyncPush = push;
function pushFn(value) {
    return iterable => push(iterable, value);
}
exports.pushFn = pushFn;
exports.asyncPushFn = pushFn;
function unshift(iterable, value) {
    return tslib_1.__asyncGenerator(this, arguments, function* unshift_1() {
        var _a, e_6, _b, _c;
        yield yield tslib_1.__await(yield tslib_1.__await(value));
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    yield yield tslib_1.__await(element);
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_6_1) { e_6 = { error: e_6_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_6) throw e_6.error; }
        }
    });
}
exports.unshift = unshift;
exports.asyncUnshift = unshift;
function unshiftFn(value) {
    return iterable => unshift(iterable, value);
}
exports.unshiftFn = unshiftFn;
exports.asyncUnshiftFn = unshiftFn;
function initial(iterable) {
    return tslib_1.__asyncGenerator(this, arguments, function* initial_1() {
        const iterator = asyncIterator(iterable);
        let prev = yield tslib_1.__await(iterator.next());
        let element = prev.done === true ? prev : yield tslib_1.__await(iterator.next());
        while (element.done !== true) {
            yield yield tslib_1.__await(prev.value);
            prev = element;
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.initial = initial;
exports.asyncInitial = initial;
async function last(iterable) {
    var _a, e_7, _b, _c;
    let last = null;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                last = element;
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_7_1) { e_7 = { error: e_7_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_7) throw e_7.error; }
    }
    return last;
}
exports.last = last;
exports.asyncLast = last;
async function only(iterable) {
    var _a, _b;
    const iterator = asyncIterator(iterable);
    const first = await iterator.next();
    return !((_a = first.done) !== null && _a !== void 0 ? _a : false) && ((_b = (await iterator.next()).done) !== null && _b !== void 0 ? _b : false) ? first.value : null;
}
exports.only = only;
exports.asyncOnly = only;
async function empty(iterable) {
    return (await asyncIterator(iterable).next()).done === true;
}
exports.empty = empty;
exports.asyncEmpty = empty;
async function notEmpty(iterable) {
    return !(await empty(iterable));
}
exports.notEmpty = notEmpty;
exports.asyncNotEmpty = notEmpty;
function slice(iterable, start = 0, end = Infinity) {
    return tslib_1.__asyncGenerator(this, arguments, function* slice_1() {
        const s = yield tslib_1.__await(start);
        const e = yield tslib_1.__await(end);
        if (e === s) {
            return yield tslib_1.__await(void 0);
        }
        const iterator = asyncIterator(iterable);
        let element = yield tslib_1.__await(iterator.next());
        for (let i = 0; i < s && element.done !== true; ++i) {
            element = yield tslib_1.__await(iterator.next());
        }
        for (let i = s; i < e && element.done !== true; ++i) {
            yield yield tslib_1.__await(element.value);
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.slice = slice;
function take(iterable, count) {
    return tslib_1.__asyncGenerator(this, arguments, function* take_1() {
        var _a, e_8, _b, _c;
        const c = yield tslib_1.__await(count);
        if (c === 0) {
            return yield tslib_1.__await(void 0);
        }
        let i = 0;
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    yield yield tslib_1.__await(element);
                    if (++i >= c) {
                        return yield tslib_1.__await(void 0);
                    }
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_8_1) { e_8 = { error: e_8_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_8) throw e_8.error; }
        }
    });
}
exports.take = take;
exports.asyncTake = take;
function takeFn(count) {
    return iterable => take(iterable, count);
}
exports.takeFn = takeFn;
exports.asyncTakeFn = takeFn;
function drop(iterable, count) {
    return tslib_1.__asyncGenerator(this, arguments, function* drop_1() {
        const iterator = asyncIterator(iterable);
        const c = yield tslib_1.__await(count);
        let element = yield tslib_1.__await(iterator.next());
        for (let i = 0; i < c && element.done !== true; ++i) {
            element = yield tslib_1.__await(iterator.next());
        }
        while (element.done !== true) {
            yield yield tslib_1.__await(element.value);
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.drop = drop;
exports.asyncDrop = drop;
function dropFn(count) {
    return iterable => drop(iterable, count);
}
exports.dropFn = dropFn;
exports.asyncDropFn = dropFn;
function takeWhile(iterable, predicate) {
    return tslib_1.__asyncGenerator(this, arguments, function* takeWhile_1() {
        var _a, e_9, _b, _c;
        let i = 0;
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    if (!(yield tslib_1.__await(predicate(element, i)))) {
                        return yield tslib_1.__await(void 0);
                    }
                    yield yield tslib_1.__await(element);
                    ++i;
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_9_1) { e_9 = { error: e_9_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_9) throw e_9.error; }
        }
    });
}
exports.takeWhile = takeWhile;
exports.asyncTakeWhile = takeWhile;
function takeWhileFn(predicate) {
    return iterable => takeWhile(iterable, predicate);
}
exports.takeWhileFn = takeWhileFn;
exports.asyncTakeWhileFn = takeWhileFn;
function takeUntil(iterable, predicate) {
    return takeWhile(iterable, async (element, index) => !(await predicate(element, index)));
}
exports.takeUntil = takeUntil;
exports.asyncTakeUntil = takeUntil;
function takeUntilFn(predicate) {
    return iterable => takeUntil(iterable, predicate);
}
exports.takeUntilFn = takeUntilFn;
exports.asyncTakeUntilFn = takeUntilFn;
function dropWhile(iterable, predicate) {
    return tslib_1.__asyncGenerator(this, arguments, function* dropWhile_1() {
        const iterator = asyncIterator(iterable);
        let element = yield tslib_1.__await(iterator.next());
        for (let i = 0; element.done !== true && (yield tslib_1.__await(predicate(element.value, i))); ++i) {
            element = yield tslib_1.__await(iterator.next());
        }
        while (element.done !== true) {
            yield yield tslib_1.__await(element.value);
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.dropWhile = dropWhile;
exports.asyncDropWhile = dropWhile;
function dropWhileFn(predicate) {
    return iterable => dropWhile(iterable, predicate);
}
exports.dropWhileFn = dropWhileFn;
function dropUntil(iterable, predicate) {
    return dropWhile(iterable, async (element, index) => !(await predicate(element, index)));
}
exports.dropUntil = dropUntil;
exports.asyncDropUntil = dropUntil;
function dropUntilFn(predicate) {
    return iterable => dropUntil(iterable, predicate);
}
exports.dropUntilFn = dropUntilFn;
exports.asyncDropUntilFn = dropUntilFn;
async function equal(a, b, elementsEqual = ordered_1.equal) {
    var _a, _b;
    const ait = asyncIterator(a);
    const bit = asyncIterator(b);
    let ar = await ait.next();
    let br = await bit.next();
    while (ar.done !== true && br.done !== true) {
        if (!(await elementsEqual(ar.value, br.value))) {
            return false;
        }
        ar = await ait.next();
        br = await bit.next();
    }
    return ((_a = ar.done) !== null && _a !== void 0 ? _a : false) && ((_b = br.done) !== null && _b !== void 0 ? _b : false);
}
exports.equal = equal;
exports.asyncEqual = equal;
function equalFn(b, elementsEqual = ordered_1.equal) {
    return async (a) => equal(a, b, elementsEqual);
}
exports.equalFn = equalFn;
exports.asyncEqualFn = equalFn;
async function notEqual(a, b, elementsEqual = ordered_1.equal) {
    return !(await equal(a, b, elementsEqual));
}
exports.notEqual = notEqual;
exports.asyncNotEqual = notEqual;
function notEqualFn(b, elementsEqual = ordered_1.equal) {
    return async (a) => notEqual(a, b, elementsEqual);
}
exports.notEqualFn = notEqualFn;
exports.asyncNotEqualFn = notEqualFn;
async function prefixMatch(a, b, elementsEqual = ordered_1.equal) {
    var _a;
    const ait = asyncIterator(a);
    const bit = asyncIterator(b);
    let ar = await ait.next();
    let br = await bit.next();
    while (ar.done !== true && br.done !== true) {
        if (!(await elementsEqual(ar.value, br.value))) {
            return false;
        }
        ar = await ait.next();
        br = await bit.next();
    }
    return (_a = br.done) !== null && _a !== void 0 ? _a : false;
}
exports.prefixMatch = prefixMatch;
exports.asyncPrefixMatch = prefixMatch;
function prefixMatchFn(b, elementsEqual = ordered_1.equal) {
    return async (a) => prefixMatch(a, b, elementsEqual);
}
exports.prefixMatchFn = prefixMatchFn;
exports.asyncPrefixMatchFn = prefixMatchFn;
function map(iterable, f) {
    return tslib_1.__asyncGenerator(this, arguments, function* map_1() {
        var _a, e_10, _b, _c;
        let i = 0;
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    yield yield tslib_1.__await(yield tslib_1.__await(f(element, i++)));
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_10_1) { e_10 = { error: e_10_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_10) throw e_10.error; }
        }
    });
}
exports.map = map;
exports.asyncMap = map;
function mapFn(f) {
    return iterable => map(iterable, f);
}
exports.mapFn = mapFn;
exports.asyncMapFn = mapFn;
function filter(iterable, predicate) {
    return tslib_1.__asyncGenerator(this, arguments, function* filter_1() {
        var _a, e_11, _b, _c;
        let i = 0;
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    if (yield tslib_1.__await(predicate(element, i++))) {
                        yield yield tslib_1.__await(element);
                    }
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_11_1) { e_11 = { error: e_11_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_11) throw e_11.error; }
        }
    });
}
exports.filter = filter;
exports.asyncFilter = filter;
function filterFn(predicate) {
    return iterable => filter(iterable, predicate);
}
exports.filterFn = filterFn;
exports.asyncFilterFn = filterFn;
function exclude(iterable, predicate) {
    return filter(iterable, async (element, index) => !(await predicate(element, index)));
}
exports.exclude = exclude;
exports.asyncExclude = exclude;
function excludeFn(predicate) {
    return iterable => exclude(iterable, predicate);
}
exports.excludeFn = excludeFn;
exports.asyncExcludeFn = exclude;
function excludeNull(iterable) {
    return filter(iterable, nullable_1.isNotNull);
}
exports.excludeNull = excludeNull;
exports.asyncExcludeNull = excludeNull;
function excludeFirst(iterable, predicate) {
    return tslib_1.__asyncGenerator(this, arguments, function* excludeFirst_1() {
        const iterator = asyncIterator(iterable);
        let element = yield tslib_1.__await(iterator.next());
        for (let i = 0; element.done !== true; ++i) {
            if (yield tslib_1.__await(predicate(element.value, i))) {
                break;
            }
            yield yield tslib_1.__await(element.value);
            element = yield tslib_1.__await(iterator.next());
        }
        if (element.done !== true) {
            element = yield tslib_1.__await(iterator.next());
        }
        while (element.done !== true) {
            yield yield tslib_1.__await(element.value);
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.excludeFirst = excludeFirst;
exports.asyncExcludeFirst = excludeFirst;
function excludeFirstFn(predicate) {
    return iterable => excludeFirst(iterable, predicate);
}
exports.excludeFirstFn = excludeFirstFn;
exports.asyncExcludeFirstFn = excludeFirstFn;
function remove(iterable, value) {
    return exclude(iterable, element => element === value);
}
exports.remove = remove;
exports.asyncRemove = remove;
function removeFn(value) {
    return iterable => remove(iterable, value);
}
exports.removeFn = removeFn;
exports.asyncRemoveFn = removeFn;
function removeFirst(iterable, value) {
    return excludeFirst(iterable, element => element === value);
}
exports.removeFirst = removeFirst;
exports.asyncRemoveFirst = removeFirst;
function removeFirstFn(value) {
    return iterable => removeFirst(iterable, value);
}
exports.removeFirstFn = removeFirstFn;
exports.asyncRemoveFirstFn = removeFirstFn;
async function fold(iterable, f, initial) {
    var _a, e_12, _b, _c;
    let accumulator = initial;
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                accumulator = await f(accumulator, element, i++);
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_12_1) { e_12 = { error: e_12_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_12) throw e_12.error; }
    }
    return accumulator;
}
exports.fold = fold;
exports.asyncFold = fold;
function foldFn(f, initial) {
    return async (iterable) => fold(iterable, f, initial);
}
exports.foldFn = foldFn;
exports.asyncFoldFn = foldFn;
async function fold1(iterable, f) {
    const iterator = asyncIterator(iterable);
    let element = await iterator.next();
    if (element.done === true) {
        throw new TypeError("fold1: empty AsyncIterable");
    }
    let accumulator = element.value;
    let i = 1;
    element = await iterator.next();
    while (element.done !== true) {
        accumulator = await f(accumulator, element.value, i++);
        element = await iterator.next();
    }
    return accumulator;
}
exports.fold1 = fold1;
exports.asyncFold1 = fold1;
function fold1Fn(f) {
    return async (iterable) => fold1(iterable, f);
}
exports.fold1Fn = fold1Fn;
exports.asyncFold1Fn = fold1Fn;
async function index(iterable, index) {
    var _a, e_13, _b, _c;
    if (index < 0 || !isFinite(index) || Math.floor(index) !== index) {
        throw new RangeError("illegal index");
    }
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                if (i++ === index) {
                    return element;
                }
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_13_1) { e_13 = { error: e_13_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_13) throw e_13.error; }
    }
    return null;
}
exports.index = index;
exports.asyncIndex = index;
function indexFn(index) {
    return async (iterable) => (0, exports.asyncIndex)(iterable, index);
}
exports.indexFn = indexFn;
exports.asyncIndexFn = indexFn;
async function contains(iterable, value) {
    var _a, e_14, _b, _c;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                if (element === value) {
                    return true;
                }
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_14_1) { e_14 = { error: e_14_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_14) throw e_14.error; }
    }
    return false;
}
exports.contains = contains;
exports.asyncContains = contains;
function containsFn(value) {
    return async (iterable) => contains(iterable, value);
}
exports.containsFn = containsFn;
exports.asyncContainsFn = containsFn;
async function indexOf(iterable, value) {
    var _a, e_15, _b, _c;
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                if (element === value) {
                    return i;
                }
                ++i;
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_15_1) { e_15 = { error: e_15_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_15) throw e_15.error; }
    }
    return null;
}
exports.indexOf = indexOf;
exports.asyncIndexOf = indexOf;
function indexOfFn(value) {
    return async (iterable) => indexOf(iterable, value);
}
exports.indexOfFn = indexOfFn;
exports.asyncIndexOfFn = indexOfFn;
async function findIndex(iterable, predicate) {
    var _a, e_16, _b, _c;
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                if (await predicate(element, i)) {
                    return i;
                }
                ++i;
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_16_1) { e_16 = { error: e_16_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_16) throw e_16.error; }
    }
    return null;
}
exports.findIndex = findIndex;
exports.asyncFindIndex = findIndex;
function findIndexFn(predicate) {
    return async (iterable) => findIndex(iterable, predicate);
}
exports.findIndexFn = findIndexFn;
exports.asyncFindIndexFn = findIndexFn;
async function find(iterable, predicate) {
    var _a, e_17, _b, _c;
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                if (await predicate(element, i++)) {
                    return element;
                }
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_17_1) { e_17 = { error: e_17_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_17) throw e_17.error; }
    }
    return null;
}
exports.find = find;
exports.asyncFind = find;
function findFn(predicate) {
    return async (iterable) => find(iterable, predicate);
}
exports.findFn = findFn;
exports.asyncFindFn = findFn;
async function maximum(iterable, compare) {
    return internalMaximum(iterable, compare !== null && compare !== void 0 ? compare : ordered_1.compare);
}
exports.maximum = maximum;
exports.asyncMaximum = maximum;
function maximumFn(compare) {
    return async (iterable) => maximum(iterable, compare);
}
exports.maximumFn = maximumFn;
exports.asyncMaximumFn = maximumFn;
async function internalMaximum(iterable, compare) {
    const iterator = asyncIterator(iterable);
    let element = await iterator.next();
    if (element.done === true) {
        return null;
    }
    let max = element.value;
    element = await iterator.next();
    while (element.done !== true) {
        if (compare(element.value, max) > 0) {
            max = element.value;
        }
        element = await iterator.next();
    }
    return max;
}
async function maximumBy(iterable, select) {
    const iterator = asyncIterator(iterable);
    let element = await iterator.next();
    if (element.done === true) {
        return null;
    }
    let max = element.value;
    let maxBy = await select(element.value, 0);
    let i = 0;
    element = await iterator.next();
    while (element.done !== true) {
        const by = await select(element.value, i++);
        if (by > maxBy) {
            max = element.value;
            maxBy = by;
        }
        element = await iterator.next();
    }
    return max;
}
exports.maximumBy = maximumBy;
exports.asyncMaximumBy = maximumBy;
function maximumByFn(select) {
    return async (iterable) => maximumBy(iterable, select);
}
exports.maximumByFn = maximumByFn;
exports.asyncMaximumByFn = maximumByFn;
async function minimum(iterable, compare) {
    return internalMaximum(iterable, (0, ordered_1.reverse)(compare !== null && compare !== void 0 ? compare : ordered_1.compare));
}
exports.minimum = minimum;
exports.asyncMinimum = minimum;
function minimumFn(compare) {
    return async (iterable) => minimum(iterable, compare);
}
exports.minimumFn = minimumFn;
exports.asyncMinimumFn = minimumFn;
async function minimumBy(iterable, select) {
    return maximumBy(iterable, async (element, index) => -(await select(element, index)));
}
exports.minimumBy = minimumBy;
exports.asyncMinimumBy = minimumBy;
function minimumByFn(select) {
    return async (iterable) => minimumBy(iterable, select);
}
exports.minimumByFn = minimumByFn;
exports.asyncMinimumByFn = minimumByFn;
async function sum(iterable) {
    return fold(iterable, (sum, element) => sum + element, 0);
}
exports.sum = sum;
exports.asyncSum = sum;
async function product(iterable) {
    return fold(iterable, (product, element) => product * element, 1);
}
exports.product = product;
exports.asyncProduct = product;
async function average(iterable) {
    const [sum, count] = await fold(iterable, ([sum], element, index) => [sum + element, index + 1], [0, 0]);
    return count === 0 ? null : sum / count;
}
exports.average = average;
exports.asyncAverage = average;
async function and(iterable) {
    return (await findIndex(iterable, element => !element)) == null;
}
exports.and = and;
exports.asyncAnd = and;
async function or(iterable) {
    return (await findIndex(iterable, Boolean)) != null;
}
exports.or = or;
exports.asyncOr = or;
async function any(iterable, predicate) {
    return (await findIndex(iterable, predicate)) != null;
}
exports.any = any;
exports.asyncAny = any;
async function all(iterable, predicate) {
    return ((await findIndex(iterable, async (element, index) => !(await predicate(element, index)))) ==
        null);
}
exports.all = all;
exports.asyncAll = all;
function concat(iterables) {
    return tslib_1.__asyncGenerator(this, arguments, function* concat_1() {
        var _a, e_18, _b, _c, _d, e_19, _e, _f;
        try {
            for (var _g = true, _h = tslib_1.__asyncValues(yield tslib_1.__await(iterables)), _j; _j = yield tslib_1.__await(_h.next()), _a = _j.done, !_a;) {
                _c = _j.value;
                _g = false;
                try {
                    const iterable = _c;
                    try {
                        for (var _k = true, iterable_1 = (e_19 = void 0, tslib_1.__asyncValues(iterable)), iterable_1_1; iterable_1_1 = yield tslib_1.__await(iterable_1.next()), _d = iterable_1_1.done, !_d;) {
                            _f = iterable_1_1.value;
                            _k = false;
                            try {
                                const element = _f;
                                yield yield tslib_1.__await(element);
                            }
                            finally {
                                _k = true;
                            }
                        }
                    }
                    catch (e_19_1) { e_19 = { error: e_19_1 }; }
                    finally {
                        try {
                            if (!_k && !_d && (_e = iterable_1.return)) yield tslib_1.__await(_e.call(iterable_1));
                        }
                        finally { if (e_19) throw e_19.error; }
                    }
                }
                finally {
                    _g = true;
                }
            }
        }
        catch (e_18_1) { e_18 = { error: e_18_1 }; }
        finally {
            try {
                if (!_g && !_a && (_b = _h.return)) yield tslib_1.__await(_b.call(_h));
            }
            finally { if (e_18) throw e_18.error; }
        }
    });
}
exports.concat = concat;
exports.asyncConcat = concat;
function prepend(a) {
    return b => concat([a, b]);
}
exports.prepend = prepend;
exports.asyncPrepend = prepend;
function append(b) {
    return a => concat([a, b]);
}
exports.append = append;
exports.asyncAppend = append;
function concatMap(iterable, f) {
    return concat(map(iterable, f));
}
exports.concatMap = concatMap;
exports.asyncConcatMap = concatMap;
function concatMapFn(f) {
    return iterable => concatMap(iterable, f);
}
exports.concatMapFn = concatMapFn;
exports.asyncConcatMapFn = concatMapFn;
async function noneNull(iterable) {
    var _a, e_20, _b, _c;
    const result = [];
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                if (element == null) {
                    return null;
                }
                result.push(element);
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_20_1) { e_20 = { error: e_20_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_20) throw e_20.error; }
    }
    return result;
}
exports.noneNull = noneNull;
exports.asyncNoneNull = noneNull;
function scan(iterable, f, initial) {
    return tslib_1.__asyncGenerator(this, arguments, function* scan_1() {
        var _a, e_21, _b, _c;
        let i = 0;
        let accumulator = initial;
        try {
            for (var _d = true, _e = tslib_1.__asyncValues(yield tslib_1.__await(iterable)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a;) {
                _c = _f.value;
                _d = false;
                try {
                    const element = _c;
                    yield yield tslib_1.__await((accumulator = yield tslib_1.__await(f(accumulator, element, i++))));
                }
                finally {
                    _d = true;
                }
            }
        }
        catch (e_21_1) { e_21 = { error: e_21_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
            }
            finally { if (e_21) throw e_21.error; }
        }
    });
}
exports.scan = scan;
exports.asyncScan = scan;
function scanFn(f, initial) {
    return iterable => scan(iterable, f, initial);
}
exports.scanFn = scanFn;
exports.asyncScanFn = scanFn;
function scan1(iterable, f) {
    return tslib_1.__asyncGenerator(this, arguments, function* scan1_1() {
        const iterator = asyncIterator(iterable);
        let element = yield tslib_1.__await(iterator.next());
        if (element.done === true) {
            return yield tslib_1.__await(void 0);
        }
        let accumulator = element.value;
        yield yield tslib_1.__await(accumulator);
        let i = 1;
        element = yield tslib_1.__await(iterator.next());
        while (element.done !== true) {
            yield yield tslib_1.__await((accumulator = yield tslib_1.__await(f(accumulator, element.value, i++))));
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.scan1 = scan1;
exports.asyncScan1 = scan1;
function scan1Fn(f) {
    return iterable => scan1(iterable, f);
}
exports.scan1Fn = scan1Fn;
function pairwise(iterable) {
    return tslib_1.__asyncGenerator(this, arguments, function* pairwise_1() {
        const iterator = asyncIterator(iterable);
        let prev = yield tslib_1.__await(iterator.next());
        if (prev.done === true) {
            return yield tslib_1.__await(void 0);
        }
        let element = yield tslib_1.__await(iterator.next());
        while (element.done !== true) {
            yield yield tslib_1.__await([prev.value, element.value]);
            prev = element;
            element = yield tslib_1.__await(iterator.next());
        }
    });
}
exports.pairwise = pairwise;
function zip(a, b) {
    return tslib_1.__asyncGenerator(this, arguments, function* zip_1() {
        const ait = asyncIterator(a);
        const bit = asyncIterator(b);
        let ar = yield tslib_1.__await(ait.next());
        let br = yield tslib_1.__await(bit.next());
        while (ar.done !== true && br.done !== true) {
            yield yield tslib_1.__await([ar.value, br.value]);
            ar = yield tslib_1.__await(ait.next());
            br = yield tslib_1.__await(bit.next());
        }
    });
}
exports.zip = zip;
exports.asyncZip = zip;
function zipFn(b) {
    return a => zip(a, b);
}
exports.zipFn = zipFn;
exports.asyncZipFn = zipFn;
async function keyBy(iterable, f) {
    var _a, e_22, _b, _c;
    var _d;
    const map = new Map();
    let i = 0;
    try {
        for (var _e = true, _f = tslib_1.__asyncValues(await iterable), _g; _g = await _f.next(), _a = _g.done, !_a;) {
            _c = _g.value;
            _e = false;
            try {
                const element = _c;
                const key = f(element, i++);
                const group = (_d = map.get(key)) !== null && _d !== void 0 ? _d : [];
                group.push(element);
                map.set(key, group);
            }
            finally {
                _e = true;
            }
        }
    }
    catch (e_22_1) { e_22 = { error: e_22_1 }; }
    finally {
        try {
            if (!_e && !_a && (_b = _f.return)) await _b.call(_f);
        }
        finally { if (e_22) throw e_22.error; }
    }
    return map;
}
exports.keyBy = keyBy;
exports.asyncKeyBy = keyBy;
function keyByFn(f) {
    return async (iterable) => keyBy(iterable, f);
}
exports.keyByFn = keyByFn;
exports.asyncKeyByFn = keyByFn;
async function keyFirstBy(iterable, f) {
    var _a, e_23, _b, _c;
    const map = new Map();
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                const key = f(element, i++);
                if (!map.has(key)) {
                    map.set(key, element);
                }
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_23_1) { e_23 = { error: e_23_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_23) throw e_23.error; }
    }
    return map;
}
exports.keyFirstBy = keyFirstBy;
exports.asyncKeyFirstBy = keyFirstBy;
function keyFirstByFn(f) {
    return async (iterable) => keyFirstBy(iterable, f);
}
exports.keyFirstByFn = keyFirstByFn;
exports.asyncKeyFirstByFn = keyFirstByFn;
async function keyLastBy(iterable, f) {
    var _a, e_24, _b, _c;
    const map = new Map();
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                map.set(f(element, i++), element);
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_24_1) { e_24 = { error: e_24_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_24) throw e_24.error; }
    }
    return map;
}
exports.keyLastBy = keyLastBy;
exports.asyncKeyLastBy = keyLastBy;
function keyLastByFn(f) {
    return async (iterable) => keyLastBy(iterable, f);
}
exports.keyLastByFn = keyLastByFn;
exports.asyncKeyLastByFn = keyLastByFn;
async function mapKeyBy(iterable, f) {
    var _a, e_25, _b, _c;
    var _d;
    const map = new Map();
    let i = 0;
    try {
        for (var _e = true, _f = tslib_1.__asyncValues(await iterable), _g; _g = await _f.next(), _a = _g.done, !_a;) {
            _c = _g.value;
            _e = false;
            try {
                const element = _c;
                const [key, value] = await f(element, i++);
                const group = (_d = map.get(key)) !== null && _d !== void 0 ? _d : [];
                group.push(value);
                map.set(key, group);
            }
            finally {
                _e = true;
            }
        }
    }
    catch (e_25_1) { e_25 = { error: e_25_1 }; }
    finally {
        try {
            if (!_e && !_a && (_b = _f.return)) await _b.call(_f);
        }
        finally { if (e_25) throw e_25.error; }
    }
    return map;
}
exports.mapKeyBy = mapKeyBy;
exports.asyncMapKeyBy = mapKeyBy;
function mapKeyByFn(f) {
    return async (iterable) => mapKeyBy(iterable, f);
}
exports.mapKeyByFn = mapKeyByFn;
exports.asyncMapKeyByFn = mapKeyByFn;
async function mapKeyFirstBy(iterable, f) {
    var _a, e_26, _b, _c;
    const map = new Map();
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                const [key, value] = await f(element, i++);
                if (!map.has(key)) {
                    map.set(key, value);
                }
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_26_1) { e_26 = { error: e_26_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_26) throw e_26.error; }
    }
    return map;
}
exports.mapKeyFirstBy = mapKeyFirstBy;
exports.asyncMapKeyFirstBy = mapKeyFirstBy;
function mapKeyFirstByFn(f) {
    return async (iterable) => mapKeyFirstBy(iterable, f);
}
exports.mapKeyFirstByFn = mapKeyFirstByFn;
exports.asyncMapKeyFirstByFn = mapKeyFirstByFn;
async function mapKeyLastBy(iterable, f) {
    var _a, e_27, _b, _c;
    const map = new Map();
    let i = 0;
    try {
        for (var _d = true, _e = tslib_1.__asyncValues(await iterable), _f; _f = await _e.next(), _a = _f.done, !_a;) {
            _c = _f.value;
            _d = false;
            try {
                const element = _c;
                const [key, value] = await f(element, i++);
                map.set(key, value);
            }
            finally {
                _d = true;
            }
        }
    }
    catch (e_27_1) { e_27 = { error: e_27_1 }; }
    finally {
        try {
            if (!_d && !_a && (_b = _e.return)) await _b.call(_e);
        }
        finally { if (e_27) throw e_27.error; }
    }
    return map;
}
exports.mapKeyLastBy = mapKeyLastBy;
exports.asyncMapKeyLastBy = mapKeyLastBy;
function mapKeyLastByFn(f) {
    return async (iterable) => mapKeyLastBy(iterable, f);
}
exports.mapKeyLastByFn = mapKeyLastByFn;
exports.asyncMapKeyLastByFn = mapKeyLastByFn;
//# sourceMappingURL=index.js.map