How to reference a generic parameter recursively?

Viewed 190

I have solved a Y-combinator problem. Just now I found that I cannot reference a generic parameter recursively.

Y = λf.(λx.f (x x)) (λx.f (x x))

for example:

IntUnaryOperator fact = Y(rec -> n -> n == 0 ? 1 : n * rec.applyAsInt(n - 1));

IntUnaryOperator Y(Function<IntUnaryOperator, IntUnaryOperator> f) {
    return g(g -> f.apply(x -> g.apply(g).applyAsInt(x)));
}

IntUnaryOperator g(G g) {
    return g.apply(g);
}

//        v--- I want to remove the middle-interface `G`
interface G extends Function<G, IntUnaryOperator> {/**/}

Q: How can I use generic parameter on the method g to avoid introducing an additional interface G, and the generic parameter should avoiding the UNCHECKED warnings?

Thanks in advance.

1 Answers
Related