import type { AnyArray, CutFirst, EmptyTuple, TupleSlices, } from "../../../types/array.mjs"; import type { AnyFunc, Func } from "../../../types/concepts.mjs"; import type { Placeholder } from "./placeholder.mjs"; import { isPlaceholder, _$_ } from "./placeholder.mjs"; type PlaceHoldedParams

= P extends EmptyTuple ? [] : [P[0] | Placeholder, ...PlaceHoldedParams>]; type CurryingCall< P extends AnyArray, R, Args extends TupleSlices> > = ApplyArgs extends EmptyTuple ? R : Currying, R>; type ApplyArgs< P extends AnyArray, Args extends TupleSlices> > = Args extends EmptyTuple ? P : CutFirst extends TupleSlices>> ? Args[0] extends Exclude ? ApplyArgs, CutFirst> : [Exclude, ...ApplyArgs, CutFirst>] : never; type Currying

= < Args extends TupleSlices> >( ...args: Args ) => CurryingCall; const internalCurrying = (fn: AnyFunc, passed: unknown[]) => (...args: unknown[]) => { const newPassed = passed.slice(); const toFill: number[] = []; for (let i = 0; i < passed.length; i++) { if (isPlaceholder(passed[i])) { toFill.push(i); } } for (let i = 0; i < args.length; i++) { const index = toFill[i]; if (index === undefined) { break; } newPassed[index] = args[i]; } if (newPassed.every((param) => !isPlaceholder(param))) { return fn(...newPassed); } else { return internalCurrying(fn, newPassed); } }; export const currying =

( fn: Func, count?: P["length"] ): Currying => { count = count ?? fn.length; const passed: unknown[] = new Array(count).fill(_$_); // @ts-expect-error Implementation detail return internalCurrying(fn, passed); };