diff --git a/src/structure/collections/iterator.ts b/src/structure/collections/iterator.ts index 4e6072234a90439ed573a5d2a7dbca45eafd91f5..8dd89ed87f23fc3115e70bcbe3539106da3dc46a 100644 --- a/src/structure/collections/iterator.ts +++ b/src/structure/collections/iterator.ts @@ -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[] { diff --git a/src/structure/spec/collections.spec.ts b/src/structure/spec/collections.spec.ts index 57e130ac83d53b26aadfd71d5f568d159a6ddc83..6ade6097f5318dee12ed28276ba8a62badbaebdd 100644 --- a/src/structure/spec/collections.spec.ts +++ b/src/structure/spec/collections.spec.ts @@ -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]); });