Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/Y-combinator/Java/y-combinator-1.java
Normal file
25
Task/Y-combinator/Java/y-combinator-1.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
: (n * f.apply(n - 1))
|
||||
);
|
||||
|
||||
System.out.println("fib(10) = " + fib.apply(10));
|
||||
System.out.println("fac(10) = " + fac.apply(10));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue