IEnumerator vs Iterator(IEnumerator vs Iterator)

IEnumerator vs Iterator

IEnumerator

不考虑方法和原始列表的可变性,和的只读向前用法:

Reset
NoveNext
Current

Initially, the enumerator is positioned before the first element in the collection. You must call the method to advance the enumerator to the first element of the collection before reading the value of ; otherwise, is undefined.

MoveNext
Current
Current

returns the same object until is called. sets to the next element.

Current
MoveNext
MoveNext
Current

If passes the end of the collection, the enumerator is positioned after the last element in the collection and returns false. When the enumerator is at this position, subsequent calls to also return false. If the last call to returned false, is undefined.

MoveNext
MoveNext
MoveNext
MoveNext
Current

输入数据,并初始化枚举器:

let ls = [0;1;2]
let enumerator = (Seq.ofList ls).GetEnumerator()
let ls = []

此时调用会抛出异常,印证第一段描述:

Current
enumerator.Current
//InvalidOperationException : 枚举尚未开始。请调用 MoveNext。

我们改正代码,连续运行,直到序列的末尾:

let ls = [0;1;2]
let enumerator = (Seq.ofList ls).GetEnumerator()
show <| enumerator.MoveNext() // true
show enumerator.Current // 0

show <| enumerator.MoveNext() // true
show enumerator.Current // 1

show <| enumerator.MoveNext() // true
show enumerator.Current // 2

当再次调用返回false,接上段代码:

MoveNext()
show <| enumerator.MoveNext() // false

当返回false之后,如果此时调用会抛出异常:

MoveNext()
Current
show enumerator.Current
// InvalidOperationException : 枚举已完成。

当返回false之后,继续调用不会抛出异常,它总是返回false:

MoveNext()
MoveNext()
show <| enumerator.MoveNext() // false
show <| enumerator.MoveNext() // false
show <| enumerator.MoveNext() // false
show <| enumerator.MoveNext() // false

当输入序列为空时,行为如下:

let ls = []
let enumerator = (Seq.ofList ls).GetEnumerator()
show <| enumerator.MoveNext() // false
show enumerator.Current
// InvalidOperationException : 枚举已完成。

Iterator

javaScript的枚举器用法如下:

const iterable = ['a', 'b'];
const iterator = iterable[Symbol.iterator]();

iterator.next() //, { value: 'a', done: false }
iterator.next() //, { value: 'b', done: false }
iterator.next() //, { value: undefined, done: true }
————————

IEnumerator vs Iterator

IEnumerator

Regardless of the variability of the method and the original list, the read-only forward usage of and:

Reset
NoveNext
Current

Initially, the enumerator is positioned before the first element in the collection. You must call the method to advance the enumerator to the first element of the collection before reading the value of ; otherwise, is undefined.

MoveNext
Current
Current

returns the same object until is called. sets to the next element.

Current
MoveNext
MoveNext
Current

If passes the end of the collection, the enumerator is positioned after the last element in the collection and returns false. When the enumerator is at this position, subsequent calls to also return false. If the last call to returned false, is undefined.

MoveNext
MoveNext
MoveNext
MoveNext
Current

Enter data and initialize the enumerator:

let ls = [0;1;2]
let enumerator = (Seq.ofList ls).GetEnumerator()
let ls = []

At this time, the call will throw an exception to confirm the description in the first paragraph:

Current
enumerator.Current
//InvalidOperationException : 枚举尚未开始。请调用 MoveNext。

We correct the code and run it continuously until the end of the sequence:

let ls = [0;1;2]
let enumerator = (Seq.ofList ls).GetEnumerator()
show <| enumerator.MoveNext() // true
show enumerator.Current // 0

show <| enumerator.MoveNext() // true
show enumerator.Current // 1

show <| enumerator.MoveNext() // true
show enumerator.Current // 2

When the call returns false again, the following code is followed:

MoveNext()
show <| enumerator.MoveNext() // false

When false is returned, the call will throw an exception:

MoveNext()
Current
show enumerator.Current
// InvalidOperationException : 枚举已完成。

When false is returned, the call will not throw an exception. It always returns false:

MoveNext()
MoveNext()
show <| enumerator.MoveNext() // false
show <| enumerator.MoveNext() // false
show <| enumerator.MoveNext() // false
show <| enumerator.MoveNext() // false

When the input sequence is empty, the behavior is as follows:

let ls = []
let enumerator = (Seq.ofList ls).GetEnumerator()
show <| enumerator.MoveNext() // false
show enumerator.Current
// InvalidOperationException : 枚举已完成。

Iterator

The usage of JavaScript enumerator is as follows:

const iterable = ['a', 'b'];
const iterator = iterable[Symbol.iterator]();

iterator.next() //, { value: 'a', done: false }
iterator.next() //, { value: 'b', done: false }
iterator.next() //, { value: undefined, done: true }