2009年9月9日水曜日

JavaでLINQ01

とりあえず無限級数。

public class Main {

public static void main(String[] args) throws InterruptedException {

for (Integer i : iterate(0, new Func1<Integer, Integer>(){
public Integer _(Integer arg0) {
return arg0 + 1;
}})) {

System.out.println("Hello, World!!" + i);
Thread.sleep(1000);
}

}

private static <T> IEnumerable<T> iterate(T initialValue, Func1<T, T> func) {

final class Result implements IEnumerable<T> {

private T initialValue;
private Func1<T, T> func;

public Result(T initialValue, Func1<T, T> func) {
this.initialValue = initialValue;
this.func = func;
}

@Override
public Iterator<T> iterator() {

final class _Result implements Iterator<T> {

private T next;
private Func1<T, T> func;

public _Result(T initialValue, Func1<T, T> func) {
this.next = initialValue;
this.func = func;
}

@Override
public boolean hasNext() {
return true;
}

@Override
public T next() {
T next = this.next;
this.next = this.func._(next);
return next;
}

@Override
public void remove() {
throw new NotImplementedException();
}

}

return new _Result(this.initialValue, this.func);
}
}

return new Result(initialValue, func);
}

}

interface Func1<T, TResult> {
TResult _(T arg0);
}

interface IEnumerable<T> extends Iterable<T> {
}

0 件のコメント:

コメントを投稿