2015-02-20 00:35:01 -05:00
|
|
|
import java.util.function.Function;
|
2013-04-11 01:07:29 -07:00
|
|
|
|
2015-02-20 00:35:01 -05:00
|
|
|
public interface YCombinator {
|
|
|
|
|
interface RecursiveFunction<F> extends Function<RecursiveFunction<F>, F> { }
|
|
|
|
|
public static <A,B> Function<A,B> Y(Function<Function<A,B>, Function<A,B>> f) {
|
|
|
|
|
RecursiveFunction<Function<A,B>> r = w -> f.apply(x -> w.apply(w).apply(x));
|
|
|
|
|
return r.apply(r);
|
|
|
|
|
}
|
2013-04-11 01:07:29 -07:00
|
|
|
|
2015-02-20 00:35:01 -05:00
|
|
|
public static void main(String... arguments) {
|
|
|
|
|
Function<Integer,Integer> fib = Y(f -> n ->
|
|
|
|
|
(n <= 2)
|
|
|
|
|
? 1
|
|
|
|
|
: (f.apply(n - 1) + f.apply(n - 2))
|
|
|
|
|
);
|
|
|
|
|
Function<Integer,Integer> fac = Y(f -> n ->
|
|
|
|
|
(n <= 1)
|
|
|
|
|
? 1
|
2019-09-12 10:33:56 -07:00
|
|
|
: (n * f.apply(n - 1))
|
2015-02-20 00:35:01 -05:00
|
|
|
);
|
2013-04-11 01:07:29 -07:00
|
|
|
|
2015-02-20 00:35:01 -05:00
|
|
|
System.out.println("fib(10) = " + fib.apply(10));
|
|
|
|
|
System.out.println("fac(10) = " + fac.apply(10));
|
|
|
|
|
}
|
2013-04-11 01:07:29 -07:00
|
|
|
}
|