Skip to content
Snippets Groups Projects
Commit fc4473e7 authored by David Sehnal's avatar David Sehnal
Browse files

simplified iterator

parent 37865407
Branches
Tags
Loading
......@@ -20,25 +20,6 @@ interface Iterator<T> {
nextValue(): T
}
class EmptyIteratorImpl implements Iterator<any> {
[Symbol.iterator]() { return this; }
done = true;
value = void 0;
next() { return this; }
nextValue() { return this.value; }
}
class SingletonIteratorImpl<T> implements Iterator<T> {
private yielded = false;
[Symbol.iterator]() { return this; }
done = false;
value: T;
next() { this.done = this.yielded; this.yielded = true; return this; }
nextValue() { return this.next().value; }
constructor(value: T) { this.value = value; }
}
class ArrayIteratorImpl<T> implements Iterator<T> {
private xs: ArrayLike<T> = [];
private index: number = -1;
......@@ -87,9 +68,9 @@ class RangeIteratorImpl implements Iterator<number> {
}
namespace Iterator {
export const Empty: Iterator<any> = new EmptyIteratorImpl();
export function Singleton<T>(value: T): Iterator<T> { return new SingletonIteratorImpl(value); }
export const Empty: Iterator<any> = new RangeIteratorImpl(0, -1);
export function Array<T>(xs: ArrayLike<T>): Iterator<T> { return new ArrayIteratorImpl<T>(xs); }
export function Value(value: number): Iterator<number> { return new RangeIteratorImpl(value, value); }
export function Range(min: number, max: number): Iterator<number> { return new RangeIteratorImpl(min, max); }
export function toArray<T>(it: Iterator<T>): T[] {
......
......@@ -17,7 +17,7 @@ describe('basic iterators', () => {
}
check('empty', Iterator.Empty, []);
check('singleton', Iterator.Singleton(10), [10]);
check('singleton', Iterator.Value(10), [10]);
check('array', Iterator.Array([1, 2, 3]), [1, 2, 3]);
check('range', Iterator.Range(0, 3), [0, 1, 2, 3]);
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment