-
-
Save aruld/3965968 to your computer and use it in GitHub Desktop.
Generic Y Combinator in Java 8 using lambdas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//based on code from http://www.arcfn.com/2009/03/y-combinator-in-arc-and-java.html and the generic version https://gist.github.com/2571928 | |
class YFact { | |
// T function returning a T | |
// T -> T | |
public static interface Func<T> { | |
T apply(T n); | |
} | |
// Higher-order function returning a T function | |
// F: F -> (T -> T) | |
private static interface FuncToTFunc<T> { | |
Func<T> apply(FuncToTFunc<T> x); | |
} | |
//Next comes the meat. We define the Y combinator, apply it to the factorial input function, and apply the result to the input argument. The result is the factorial. | |
// Formulation : λr.(λf.(f f)) λf.(r λx.((f f) x)) | |
public static <T> Func<T> Y(final Func<Func<T>> r) { | |
return ((FuncToTFunc<T>) f -> f.apply(f)) | |
.apply( | |
f -> r.apply( | |
x -> f.apply(f).apply(x))); | |
} | |
public static void main(String args[]) { | |
System.out.println( | |
// Y combinator | |
Y( | |
// Recursive function generator | |
new Func<Func<Integer>>() { | |
public Func<Integer> apply(final Func<Integer> f) { | |
return n -> n == 0 ? 1 : n * f.apply(n - 1); | |
} | |
} | |
).apply( | |
// Argument | |
Integer.parseInt(args[0]))); | |
} | |
} |
I found this easier to understand by naming the function interfaces uniquely as the order got higher.
import java.util.function.Function;
public class YCombinator {
interface Hopper<T,R> {
Function<T,R> hop(Function<T,R> inFunc);
}
interface Fixer<T,R> {
Function<T,R> fix(Hopper<T,R> toFix);
}
interface SelfApply<X> {
X self(SelfApply<X> me);
}
static <T,R> SelfApply<Fixer<T,R>> combinator() {
return me -> hopper -> input -> hopper.hop(me.self(me).fix(hopper)).apply(input);
}
static <T,R> Fixer<T,R> fixer() {
final SelfApply<Fixer<T,R>> y = combinator();
return y.self(y);
}
public static void main(String[] args) {
final Hopper<Integer,Integer> factorialDefinition = deeper ->
n -> (n > 0 ? n * deeper.apply(n - 1) : 1);
final Fixer<Integer, Integer> fixer = fixer();
final Function<Integer,Integer> factorial = fixer.fix(factorialDefinition);
for (int i = 0; i < 12; i++) {
System.out.printf("%3d => %d\n", i, factorial.apply(i));
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice Sam! Yea, I run into type error with the latter approach. But, it can be fixed by providing a type hint like before.
Btw, I like Brian's version which looks much better.