rivo
Version:
🤖 The ultimate library you need for composable type-level programming in TypeScript, powered by HKT.
56 lines (52 loc) • 2.14 kB
TypeScript
import type { List } from ".";
import type { Args, Call2W, Fn2, GenericFn, GenericResolver, Param0, Param1, Return } from "../HKT";
import type { Broaden } from "../helpers";
/**
* Apply a binary function to each element of a {@link List} (i.e., fixed-length tuple), starting
* from the **right**. The result of the previous application is passed as the first argument to the
* next application.
*
* The **R** suffix stands for “**R**ight”.
*
* Sig: `<T, U>(f: (acc: U, x: T) => U, init: U, xs: List<T>) => U`
*/
export type FoldR<F extends Fn2, I, TS extends List> =
number extends TS["length"] ? Return<F>
: TS extends readonly [infer Head, ...infer Tail] ?
FoldR<F, I, Tail> extends infer R ?
Call2W<F, R, Head>
: never
: I;
interface Resolver extends GenericResolver<[Fn2, unknown, List], unknown> {
on1__: ([f]: Args<this>) => [
[Param0<typeof f> | Return<typeof f>, List<Param1<typeof f>>],
Param0<typeof f> | Return<typeof f>,
];
on_1_: ([, i]: Args<this>) => [
[Fn2<Broaden<typeof i>, never, Broaden<typeof i>>, List],
Broaden<typeof i>,
];
on__1: ([, , xs]: Args<this>) => [[Fn2<never, Broaden<(typeof xs)[number]>>, unknown], unknown];
on11_: ([f, i]: Args<this>) => [[List<Param1<typeof f>>], Param0<typeof f> | Return<typeof f>];
on1_1: ([f, , xs]: Args<this>) => [
[Param0<typeof f> | Return<typeof f>],
Param0<typeof f> | Return<typeof f>,
];
on_11: ([, i, xs]: Args<this>) => [
[Fn2<Broaden<typeof i>, (typeof xs)[number], Broaden<typeof i>>],
Broaden<typeof i>,
];
on111: ([f, i, xs]: Args<this>) => [[], Param0<typeof f> | Return<typeof f>];
}
/**
* [Fn] Apply a binary function to each element of a {@link List} (i.e., fixed-length tuple),
* starting from the **right**. The result of the previous application is passed as the first
* argument to the next application.
*
* The **R** suffix stands for “**R**ight”.
*
* Sig: `<T, U>(f: (acc: U, x: T) => U, init: U, xs: List<T>) => U`
*/
export default interface FoldRFn extends GenericFn<Resolver> {
def: ([f, i, xs]: Args<this>) => FoldR<typeof f, typeof i, typeof xs>;
}