From fc4473e7410a03630b638ac087caf6e64b6a3508 Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Fri, 20 Oct 2017 02:24:30 +0200
Subject: [PATCH] simplified iterator

---
 src/structure/collections/iterator.ts  | 23 ++---------------------
 src/structure/spec/collections.spec.ts |  2 +-
 2 files changed, 3 insertions(+), 22 deletions(-)

diff --git a/src/structure/collections/iterator.ts b/src/structure/collections/iterator.ts
index 4e6072234..8dd89ed87 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 57e130ac8..6ade6097f 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]);
 });
-- 
GitLab