-
Notifications
You must be signed in to change notification settings - Fork 31
/
Iterator.cs
221 lines (187 loc) · 7.08 KB
/
Iterator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
namespace DesignPatterns.Behavioral;
public partial class Iterator
{
[Fact]
public void Execute()
{
IAggregate<string> sentence = new Sentence("The quick brown fox jumps over the lazy dog.");
IIterator<string> sentenceIterator = sentence.CreateIterator();
// To iterate the words in the sentence:
// while (sentenceIterator.HasNext())
// {
// string word = sentenceIterator.Next();
// }
Assert.NotNull(sentenceIterator);
Assert.True(sentenceIterator.HasNext());
Assert.Equal("The", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("quick", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("brown", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("fox", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("jumps", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("over", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("the", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("lazy", sentenceIterator.Next());
Assert.True(sentenceIterator.HasNext());
Assert.Equal("dog", sentenceIterator.Next());
Assert.False(sentenceIterator.HasNext());
IEnumerable<string> sentenceAsEnumerable = new SentenceAsEnumerable("The quick brown fox jumps over the lazy dog.");
Assert.Equal(["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"], sentenceAsEnumerable);
// To iterate over an Enumerable:
//
// using (IEnumerator<string> sentenceEnumerator = sentenceAsEnumerable.GetEnumerator())
// {
// while (sentenceEnumerator.MoveNext())
// {
// string word = sentenceEnumerator.Current;
//
// }
// }
//
// or simply:
//
// foreach (string word in sentenceAsEnumerable)
// {
//
// }
}
/// <summary>
/// Iterator
/// </summary>
/// <remarks>
/// Defines an interface for accessing and traversing elements.
/// </remarks>
/// <typeparam name="T">
/// Covariance (out) enables you to use a more derived type than originally specified.
/// You can assign an instance of <see cref="IIterator{Derived}" /> to a variable of type <see cref="IIterator{Base}" />.
/// </typeparam>
public interface IIterator<out T>
{
bool HasNext();
T Next();
}
/// <summary>
/// Aggregate
/// </summary>
/// <remarks>
/// Defines an interface for creating an Iterator object.
/// </remarks>
/// <typeparam name="T">
/// Covariance (out) enables you to use a more derived type than originally specified.
/// You can assign an instance of <see cref="IAggregate{Derived}" /> to a variable of type <see cref="IAggregate{Base}" />.
/// </typeparam>
public interface IAggregate<out T>
{
IIterator<T> CreateIterator();
}
/// <summary>
/// Concrete Aggregate
/// </summary>
/// <remarks>
/// Implements the Iterator creation interface to return an instance of the proper Concrete Iterator.
/// </remarks>
public class Sentence(string sentence) : IAggregate<string>
{
private string[] Words { get; } = SplitWords(sentence);
public IIterator<string> CreateIterator() => new WordsIterator(this);
/// <summary>
/// Concrete Iterator
/// </summary>
/// <remarks>
/// - Implements the Iterator interface.
/// - Keeps track of the current position in the traversal of the aggregate.
/// </remarks>
private sealed class WordsIterator(Sentence sentence) : IIterator<string>
{
private int _index = 0;
public bool HasNext() => sentence.Words.Length > _index;
public string Next() => HasNext() ? sentence.Words[_index++] : null;
}
}
/// <summary>
/// Concrete Aggregate
/// </summary>
/// <remarks>
/// In C#, IEnumerable is considered the equivalent of the Aggregate.
/// Using this interface will allow you to use default features in C#, like foreach and linq expressions.
/// </remarks>
public class SentenceAsEnumerable(string sentence) : IEnumerable<string>
{
private string[] Words { get; } = SplitWords(sentence);
/// <summary>
/// Creates the IEnumerator object (Iterator).
/// </summary>
/// <returns>
/// Generic Iterator,
/// </returns>
public IEnumerator<string> GetEnumerator() => new WordsEnumerator(this);
/// <summary>
/// The non-generic IEnumerable asks for an object iterator, we can simply return the same iterator that we already have
/// implemented.
/// </summary>
/// <returns>
/// Object Iterator
/// </returns>
/// <remarks>
/// When you implement multiple interfaces and they contain the same properties/methods name but with different types,
/// you will have to explicitly implement them.
/// Writing the {InterfaceName}.{MethodName | PropertyName} is considered as an explicit implementation.
/// </remarks>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Concrete Iterator
/// </summary>
/// <remarks>
/// In C#, IEnumerator is the equivalent of the Iterator.
/// </remarks>
private sealed class WordsEnumerator : IEnumerator<string>
{
private readonly SentenceAsEnumerable _sentence;
private int _index;
public WordsEnumerator(SentenceAsEnumerable sentence)
{
_sentence = sentence;
_index = 0;
Current = default;
}
public bool MoveNext()
{
if (_sentence.Words.Length > _index)
{
Current = _sentence.Words[_index++];
return true;
}
return false;
}
public void Reset()
{
_index = 0;
Current = default;
}
public string Current { get; private set; }
/// <summary>
/// IEnumerator's interface asks for an object for the current Property, you can simply return our other property.
/// </summary>
object IEnumerator.Current => Current;
public void Dispose()
{
// Nothing to dispose
}
}
}
private static string[] SplitWords(string sentence)
{
return WordExpression()
.Matches(sentence ?? "")
.Select(match => match.Value)
.ToArray();
}
[GeneratedRegex("[^ .,']+")]
private static partial Regex WordExpression();
}