import { assert } from "./asserts.js";
/**
 * Returns `arr` as is if none of the elements are `undefined`.
 * Otherwise returns a new array with only defined elements in `arr`.
 */
export function defined(arr) {
    // avoid an array copy if possible
    let i = arr.findIndex(x => x === undefined);
    if (i < 0) {
        return arr;
    }
    const defined = arr.slice(0, i);
    for (i++; i < arr.length; i++) {
        const x = arr[i];
        if (x !== undefined) {
            defined.push(x);
        }
    }
    return defined;
}
export function areEqual(arr1, arr2) {
    return arr1.length === arr2.length && arr1.every((e, i) => e === arr2[i]);
}
export function zip(a1, a2) {
    assert(a1.length === a2.length);
    const result = [];
    for (let i = 0; i < a1.length; i++) {
        result.push([a1[i], a2[i]]);
    }
    return result;
}
export function last(arr) {
    if (arr.length === 0) {
        return undefined;
    }
    return arr[arr.length - 1];
}
export function groupBy(arr, keyFn) {
    const groups = new Map();
    for (const el of arr) {
        const key = keyFn(el);
        let group = groups.get(key);
        if (group === undefined) {
            group = [];
            groups.set(key, group);
        }
        group.push(el);
    }
    return groups;
}
//# sourceMappingURL=arrays.js.map