Declarative Objectivity (DO) Language : Expressions : Collection-Element Expressions
Collection-Element Expressions
A collection-element expression evaluates to one or more element values of a collection. Collection-element expressions can be used as components of Expressions for Reachable Values.
Syntax 
collectionElement : collectionSpecifier '[' subscript ']';
collectionSpecifier : attribute | collectionElement;
subscript : indexSubscript | rangeSubscript | predicateSubscript ;
rangeSubscript : startIndex ':' limitIndex;
Quick Look 
Set the fourth date in the rental company’s list of high-volume dates:
UPDATE RentalCompany SET highVolumeDates[3] TO 2017-04-30;
Clear the first, second, and third elements from a vehicle’s list of service records:
UPDATE Vehicle WHERE license == 'ABC1997' CLEAR servRecs[0:3];
Return the number of vehicles in the rental company that are currently in service:
FROM RentalCompany RETURN COUNT(vehicles[retired == false]) as NumVehiclesInService;
Discussion 
A collection-element expression returns one or more elements of a collection associated with the current context object. You can combine collection-element expressions in more complex expressions to designate values that are reachable through collection elements; see Expressions for Reachable Values.
A collection-element expression is a subscript operator expression in which:
The first operand is a collectionSpecifier expression that evaluates to a collection (list, set, or map).
The second operand is an expression specifying how elements are selected from the collection—an Index Subscript, a Range Subscript, or a Predicate Subscript.
A collection-element expression returns either a single value or a sequence of values, depending on the subscript expression. In either case, the returned value or values are of the element-type specified by the collection. The returned values could be simple (such as Boolean, numeric, string, date, time, or datetime values) or complex (such as object references, embedded objects, or even collections).
The collectionSpecifier expression is typically an attribute expression. For example, in the following statement, the collection of interest is obtained by accessing the RentalCompany’s highVolumeDates attribute:
UPDATE RentalCompany SET highVolumeDates[3] TO 2017-04-30;
 
Index Subscript
When collectionSpecifier evaluates to a list of ordered elements, you can obtain the value of the element at a particular index by specifying an indexSubscript expression. An index is an integer that describes an element’s position in the ordering of the elements in the list. Index subscripts can be used only with lists; see Subscripts and Collection Types.
 
Value of indexSubscript 1 
Description
Example
Returns
Positive integer
0-based index that counts forward from the beginning of the list. The first element of the list has index 0.
rentals[0]
First element
rentals[1]
Second element
rentals[2]
Third element
Negative integer
Index that counts backward from the end of the list. The last element of the list has index -1.
rentals[-1]
Last element
rentals[-2]
Next to last element
rentals[-3]
Third from last element
1. If indexSubscript indicates a position outside the list, the expression returns a null value.
Example. When the following statement processes the qualifying Customer, the expression in the CLEAR clause evaluates to the first element of the collection accessed through the Customer’s rentals attribute; the statement clears just that collection element:
UPDATE Customer WHERE lastName == 'Mertz' CLEAR rentals[0];
Example. When the following statement processes the qualifying Vehicle, the expression in the RETURN clause returns the last element in Vehicle’s pastRentals collection.
FROM Vehicle WHERE license == 'ABC1997' RETURN pastRentals[-1];
Range Subscript
When collectionSpecifier evaluates to a list of ordered elements, you can obtain the values of consecutive elements within a range of index positions by specifying a rangeSubscript expression. A range of elements is sometimes called a slice of a list. Ranges subscripts can be used only with lists; see Subscripts and Collection Types.
A rangeSubscript expression is a colon-separated pair of expressions that evaluate to integers. The integers m:n identify a range of indexes as follows:
The first integer (m) is the index of the element that starts the range.
The second integer (n) is the index that limits the range. The range extends up to the nth index position, but does not include it.
A positive m or n designates a 0-based index by counting forward from the beginning of the list. The first element of the list has index 0.
A negative m or n designates an index by counting backward from the end of the list. The last element of the list has index -1.
 
Value of rangeSubscript 1
Description
Example
Returns
m:n
Range of elements from indexes m through n-1.
rentals[0:3]
First 3 elements
rentals[1:3]
Second and third elements
rentals[-3:-1]
Second and third elements from the end
rentals[1:-1]
All elements except the first and last
:n
Range of elements from the index 0 (beginning of the list) through index n-1.
rentals[:3]
First 3 elements
rentals[:-3]
First element up to, but not including, the third element from the end.
m:
Range of elements from index m through the end of the list.
rentals[-3:]
Last 3 elements
rentals[4:]
Fifth through the last element
1. If either m or n indicates an index position outside the list, it is automatically adjusted to be the nearest valid position.
Within a rangeSubscript expression m:n, the element at the first index m should be closer to the beginning of the list than the element at the second index n. Consequently, you normally want m < n when both indexes are either positive or negative; otherwise, the expression returns an empty range:
rentals[3:1]    // both positive; m>n; range is empty
rentals[-1:-3]  // both negative; m>n; range is empty
If m and n have different signs, m may be larger than n, as long as m designates an element that is closer to the beginning of the list than n. For example, the following expression describes a range that starts from the list’s third element (index 2) and extends up to (but not including) the third element from the end. The range is not empty if the rentals list has six or more elements.
rentals[2:-3]    // mixed sign; m>n; nonempty in large list
Predicate Subscript
When collectionSpecifier evaluates to a collection whose elements are embedded objects or references to objects, you can specify a predicateSubscript expression to select just the objects that meet some qualifying condition. Each embedded or referenced object in the collection is tested against the predicateSubscript expression, which typically compares an attribute value to some other specified value. The predicateSubscript expression evaluates to true if the object qualifies (meets the specified condition), and false if it does not.
A collection-element expression with a predicateSubscript returns a sequence of qualifying objects or references (object identifiers). The sequence is empty if no objects qualify.
Example. When the following statement processes the RentalCompany, the expression in the RETURN clause tests each VehicleModel in the RentalCompany’s models collection, and returns a sequence of references to the VehicleModel objects that satisfy the Boolean expression seatingCapacity > 6.
FROM RentalCompany RETURN models[seatingCapacity > 6];
Testing Map Values
When collectionSpecifier evaluates to a map of key/value pairs, the predicateSubscript expression qualifies just the value portion of each pair.
Example. A RentalCompany’s customers collection is a name map that pairs string keys to values that are references to Customer objects. When the following statement processes the RentalCompany, the expression in the RETURN clause tests just the referenced Customer objects in the RentalCompany’s customers collection; the string keys are ignored. The statement returns a sequence of references to the Customer objects that satisfy the Boolean expression rewardPoints > 1000.
FROM RentalCompany RETURN customers[rewardPoints > 1000];
Subscripts and Collection Types
Within a collection-element expression, the subscript you can use for selecting elements depends on whether the collectionSpecifier is a list, set, or map:
You can select elements from a list by using an Index Subscript, a Range Subscript, or a Predicate Subscript.
You can select elements from a set or map by using a Predicate Subscript.
In general, indexes and ranges of indexes are meaningful only with lists, because a list maintains its elements in the order in which you add them. In contrast, sets and maps use internal algorithms to control the ordering of their elements. Consequently, the position of a particular element within a set or map is always subject to change, and cannot be consistently identified by index positions.