The Pair data type is decribed in Section 10.3.6.
10.3.3 Sequences
A sequence is an ordered collection of (possibly repeating) elements. This is similar
to the .NET List type, except that a sequence is an immutable value. Operations to
add and remove elements result in new sequences instead of making changes to the
sequence given as the argument.
Another difference between sequences and the .NET list type is structural equality.
Two sequences are equal if they contain the same elements, in the same order. Lists
in .NET use reference equality instead of structural equality.
Appendix A.2.4 provides a reference of sequence operations provided by the
NModel library.
Systems with Complex State 165
Sequences appear in models where it is important to distinguish elements based on
order. For example, you could model a variable-length stack (LIFO) with operations
Push and Pop using a sequence:
static class Stack
{
static Sequence
contents = Sequence.EmptySequence;
[Action]
static void Push(int value)
{
contents = contents.AddFirst(value);
}
static bool PopEnabled() { return !contents.IsEmpty; }
[Action]
static int Pop()
{
int result = contents.Head;
contents = contents.Tail;
return result;
}
}
Order matters in this example: Pop must produce the value given by the most
recent Push action.
Creating sequences
You can create a sequence by listing its elements as arguments to the constructor:
Sequence ratings = new Sequence("Poor", "Average",
"Excellent")
You can also start with the empty sequence and add elements to the beginning or
end programmatically:
Sequence squares = Map.
Pages:
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234