y combinator named y combinator?2025 (Source: ChatGPT) (The Y cominator problem)
const Y = fun => {
const f = x => {
return fun(v => {
return x(x)(v);
});
};
return (f)(f);
};
// Factorial
const factorial = Y(recursiveFun => {
return n => {
// Note: `n` is a variable that represents the position (or index)
// of a term in the sequence.
return n === 0 ? 1 : n * recursiveFun(n - 1);
};
});
console.log(factorial(5)); // 120