|
| 1 | +# Iterator |
| 2 | + |
| 3 | +## Motivation |
| 4 | + |
| 5 | +Knows how to traverse over an object |
| 6 | +- Keep track of current element. |
| 7 | +- Knows how to move to next object. |
| 8 | + |
| 9 | +## Iterator as an Object |
| 10 | + |
| 11 | +The classic approach of an Iterator is making it an object for the enumeration of certain type. |
| 12 | +Since we're using .NET, we'll implement it with `IEnumerator` and `IEnumerator<T>`. |
| 13 | + |
| 14 | +Let's take a look of `IEnumerator`: |
| 15 | + |
| 16 | +```cs |
| 17 | +public interface IEnumerator |
| 18 | +{ |
| 19 | + object Current { get; } |
| 20 | + bool MoveNext(); // change state and tell whether can move to next item. |
| 21 | + void Reset(); // reset to initial state before iteration. |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Back in the early days when `C#` doesn't have generic types, `IEnumerator` was designed as a reconciled way. |
| 26 | + |
| 27 | +Later we got `IEnumerator<T>` which simply overrides `Current` with concrete type. |
| 28 | + |
| 29 | +```cs |
| 30 | +public interface IEnumerator<out T> : IEnumerator, IDisposable |
| 31 | +{ |
| 32 | + T Current { get; } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +> [!NOTE] |
| 37 | +> `IEnumerator<T>` extends `IDisposable` for making sure the object being iterated can be collected by GC later on. |
| 38 | +
|
| 39 | +```cs |
| 40 | +int[] foo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; |
| 41 | + |
| 42 | +ArrayEnumerator<int> enumerator = new(foo); |
| 43 | + |
| 44 | +while (enumerator.MoveNext()) |
| 45 | +{ |
| 46 | + Console.WriteLine(enumerator.Current); |
| 47 | +} |
| 48 | + |
| 49 | +class ArrayEnumerator<T> : IEnumerator<T> |
| 50 | +{ |
| 51 | + private readonly T[] _source = []; |
| 52 | + private int _cursor; |
| 53 | + public ArrayEnumerator(T[] source) |
| 54 | + { |
| 55 | + _source = source; |
| 56 | + Current = source[0]; |
| 57 | + } |
| 58 | + |
| 59 | + public T Current { get; private set; } |
| 60 | + |
| 61 | + object IEnumerator.Current => Current; |
| 62 | + |
| 63 | + |
| 64 | + public bool MoveNext() |
| 65 | + { |
| 66 | + if (_cursor < _source.Length) |
| 67 | + { |
| 68 | + Current = _source[_cursor++]; |
| 69 | + return true; |
| 70 | + } |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + public void Reset() |
| 75 | + { |
| 76 | + _cursor = 0; |
| 77 | + } |
| 78 | + |
| 79 | + public void Dispose() { } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +> [!tip] |
| 84 | +> It'e pretty ahrd to implement recursive logic in an iterator object, use iterator method instead if you do want it. |
| 85 | +
|
| 86 | +## Iterator as a Method |
| 87 | + |
| 88 | +.NET can auto generates state machine for each method yield returns `IEnumerable<T>`. |
| 89 | + |
| 90 | +```cs |
| 91 | +using System.Collections; |
| 92 | + |
| 93 | +int[] foo = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; |
| 94 | +foreach (var item in EnumerateArray(foo)) |
| 95 | +{ |
| 96 | + Console.WriteLine(item); |
| 97 | +} |
| 98 | +// pointless but that's ok |
| 99 | +static IEnumerable<T> EnumerateArray<T>(T[] source) |
| 100 | +{ |
| 101 | + for (int i = 0; i < source.Length; i++) |
| 102 | + yield return source[i]; |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +> [!TIP] |
| 107 | +> You can have multiple getter as Iterator methods, representing different ways of enumeration. |
| 108 | +
|
| 109 | +If you do want the type has an overall solution for enumeration, just implement a `GetEnumerator` method. |
| 110 | +`foreach` and `GetEnumerator` now supports duck typing, meaning that: |
| 111 | + |
| 112 | +- An object implemented `GetEnumerator` can be enumerated by `foreach` |
| 113 | +- The return type of `GetEnumerator` can be valid as long as it satisfies the shape of `IEnumerator<T>` |
| 114 | + |
| 115 | +This feature is typically for `ref struct`. |
| 116 | + |
| 117 | +## Conclusion |
| 118 | + |
| 119 | +- Use `IEnumerator<T>` to implement an Iterator class. |
| 120 | +- Use `IEnumerable<T>` as return type for Iterator methods. |
| 121 | +- Implement `GetEnumerator` to mark a the type to handle iteration by itself. |
0 commit comments