Created
April 9, 2009 16:52
-
-
Save moriyoshi/92586 to your computer and use it in GitHub Desktop.
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
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