Y combinator

asin:4320122089 を読んでたら不動点演算子が出てきて組みたくなったのでJavaで久しぶりに書いてみた。

interface Command 
{
	public void exec(Command self, Object[] args);
}

public class YCombinatorTest
{
	public static void main(String args[])
	{
		int argValue = 10;
		if (args.length == 1)
			argValue = Integer.parseInt(args[0]);
			
		new Command() {
			public void exec(Command executor, Object[] args) {
				executor.exec(executor, args);
			}
		}.exec(new Command() {
				public void exec(Command self, Object[] args)
				{
					assert args.length == 2;

					int value = ((Integer)args[0]).intValue();
					assert value > 0;
					int contValue = ((Integer)args[1]).intValue();

					if (value == 1)
						System.out.println("value: " + contValue);
					else {
						self.exec(self, new Object[] {
                new Integer(value - 1),
                new Integer(value * contValue)});
          }
        }
      }, new Object[] {
        new Integer(argValue),
        new Integer(1) });
  }

}