Perfect Developer basic tutorial 3 This page last modified 2011-10-29 (JAC)

Collection classes

Objects belonging to collection classes hold multiple values, rather like arrays in programming languages.

There are three basic collection classes available in Perfect. These are set of X, bag of X and seq of X.

The parameter X is a placeholder for whatever class you wish to have a collection of, so you can have for example seq of int, seq of char (which is also known by the name string), or seq of BankAccount (provided you declare the class BankAccount).

An object of class set of X holds an unordered collection of objects of class X with duplicates prohibited.

Class bag of X represents a similar collection except that duplicates (and multiple instances in general) are permitted.

An object of class seq of X is like a bag of X except that it is ordered, so we can enquire what value is at a particular position in the sequence (just like indexing into an array).

The constructors for each of these class takes a list of objects of type X.
For example set of int{1, 3, 5}.

To build an empty collection, just use an empty parameter list, e.g. seq of real{}.

All three of these collection classes implement the unary "#" operator. This operator returns the number of elements in the collection as an int and a member function empty which returns true if the length is zero.
For example, # set of int{1, 3, 5} yields 3.

To test whether a value occurs in a collection, use the binary in operator (which returns bool); e.g. 3 in set of int{1, 3, 5} yields true. The bag and seq classes also provide a binary "#" operator for counting the number of times an element occurs.
For example, 3 # bag of int{1, 2, 3, 1, 3, 1} has the value 2.

To add an element to a collection (yielding a new collection of the same type of object), use the append member function. In the case of seq the new element is added at the end of the sequence (e.g. seq of int{1, 2}.append(3) = seq of int{1,2,3} ). To add an element at the beginning of a sequence use the prepend member instead.

To combine two collections to form a new collection containing all the elements from the originals, use the "++" operator. This operation is called uniting for set and bag operands, or concatenation for seq operands.

You can remove individual elements from a set or bag using the remove member function, or whole groups of elements using the "--" (set or bag difference) operator.

To find all the elements that occur in two sets or two bags, use the "**" (intersection) operator.
For example,  set of int{1, 2, 3} ** set of int{5, 3, 4} = set of int{3}.

To test whether one set or bag is contained in another, use the "<<" or "<<=" operator. When these operators are used between sequences, they test whether the first operand is a subsequence of the second. The difference between the two operators is that "<<=" also returns true if the collections are equal, whereas "<<" does not.

You can also use ">>" or ">>=" which behave the same as "<<" and "<<=" but take the operands in the opposite order.

You've probably already guessed that all three of these collection classes are query final!

Knowledge round-up quiz

Next:  Bound Variables

 

Save My Place Glossary Language Reference Manual
Tutorials Overview Main site   
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice.