variadic-y
v2.0.1
Published
Variadic y-combinator for recursive anonymous functions
Downloads
247,052
Maintainers
Readme
variadic-y
Variadic y-combinator for recursive anonymous functions.
Installation
npm i variadic-y
Usage
Define your recursive function to take in a reference to itself. Here, f
.
import { Y } from "variadic-y";
Y(f => (x, y = 1) => (x == 1 ? y : f(x - 1, x * y)))(5); // 120
Design
Unmemoized to enable usage with objects and referentially opaque functions.
export const Y = (a: any) =>
((b: any) => a((...c: any[]) => b(b)(...c)))((b: any) =>
a((...c: any[]) => b(b)(...c))
);
If anyone knows how to properly type this combinator in TypeScript, let me know.