thi-ng/umbrella

View on GitHub
packages/transducers/src/max-compare.ts

Summary

Maintainability
A
3 hrs
Test Coverage
import type { Comparator, Fn0 } from "@thi.ng/api";
import { compare } from "@thi.ng/compare/compare";
import type { Reducer } from "./api.js";
import { $$reduce, reducer } from "./reduce.js";

export function maxCompare<T>(init: Fn0<T>, cmp?: Comparator<T>): Reducer<T, T>;
export function maxCompare<T>(init: Fn0<T>, src: Iterable<T>): T;
export function maxCompare<T>(
    init: Fn0<T>,
    cmp: Comparator<T>,
    src: Iterable<T>
): T;
export function maxCompare(...args: any[]): any {
    const res = $$reduce(maxCompare, args);
    if (res !== undefined) {
        return res;
    }
    const init = args[0];
    const cmp: Comparator<any> = args[1] || compare;
    return reducer(init, (acc, x) => (cmp(acc, x) >= 0 ? acc : x));
}