Skip to content

Instantly share code, notes, and snippets.

@moriyoshi
Created April 9, 2009 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moriyoshi/92586 to your computer and use it in GitHub Desktop.
Save moriyoshi/92586 to your computer and use it in GitHub Desktop.
interface MyCallable<T> {
T call(Object[] args);
}
interface CallableFactory<T> {
MyCallable<T> create();
}
interface CallableFactoryProvider<T> {
CallableFactory<T> create(Object[] params);
}
class FixedPoint<T> implements CallableFactory<T> {
public MyCallable<T> create() {
System.out.println("inner");
return cfp.create(cfp_args).create();
}
public FixedPoint(CallableFactoryProvider<T> cfp) {
this.cfp = cfp;
this.cfp_args = new Object[] { new Applier<T>(this) };
}
CallableFactoryProvider<T> cfp;
Object[] cfp_args;
}
class Applier<T> implements MyCallable<T> {
public T call(Object[] args) {
return proc.create().call(args);
}
public Applier(FixedPoint<T> proc) {
this.proc = proc;
}
final FixedPoint<T> proc;
}
public class Y {
public static void main(String[] args) {
FixedPoint<Integer> y = new FixedPoint<Integer>(
new CallableFactoryProvider<Integer>() {
public CallableFactory<Integer> create(Object[] params) {
final MyCallable<Integer> applier =
(MyCallable<Integer>)params[0];
return new CallableFactory<Integer>() {
public MyCallable<Integer> create() {
return new MyCallable<Integer>() {
public Integer call(Object[] args) {
assert args[0] instanceof Integer;
return (Integer)args[0] <= 1? 1:
applier.call(new Object[] { (Integer)args[0] - 1 }) * (Integer)args[0];
}
};
}
};
}
}
);
System.out.println(y.create().call(new Object[] { 10 }));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment