Previous | Table of Contents | Next |
The first and last elements may be directly requested. Elements can be accessed based on the location relative to other elements or based on search criteria specified using a block:
abcdefghijklmnopqrstuvwxyz last $z #(alpha beta gamma) before: gamma beta #(alpha beta gamma) findFirst: [:elem| elem size = 5] alpha #(alpha beta gamma) findLast: [:elem| elem size = 5] gamma
The index of messages support searching for the index position of specific objects or subsequences of objects. If the desired object or subsequence is not found, an index of 0 is returned. Alternative forms allow the specification of a block that is evaluated if the search is not successful:
#(alpha beta gamma) indexOf: gamma startingAt: 1 3 #(alpha beta gamma) indexOf: alpha startingAt: 2 0 abcdefghijklmnopqrstuvwxyz indexOfSubCollection: xyz startingAt: 1 ifAbsent: [self error: not found] 24
The comma binary selector is used to concatenate one named <sequencedReadableCollection> with another, producing a new collection. copyFrom:to: is used to create a new collection containing a subsequence of the values. reverse creates a new collection of the elements in reverse order:
abc , 123 abc123 abc123 copyFrom: 2 to: 5 bc12 #(alpha beta gamma) reverse #(gamma beta alpha)
The messages copyWith: is used to create a new collection that appends a single element. copyWithout: copies the elements of its receiver to a new collection and excludes a particular element. copyReplaceFrom:to:with: makes a copy while replacing a specific subsequence, and copyReplaceAll:with: makes a copy that replaces one subsequence of elements with another:
#(alpha beta gamma) copyWith: delta #(alpha beta gamma delta) remove blanks from this string copyWithout: Character blank removeblanksfromthisstring abc123 copyReplaceFrom: 2 to: 5 with: XX aXX3 , <sequenceReadableCollection> ^ <sequenceReadableCollection> copyReplaceAll: <sequenceReadableCollection> with: <sRC> , <sRC> ^ <sRC>
<sequencedReadableCollection> defines several additional iteration messages in addition to the iterators defined by <collection>. reverseDo: operates just like do:, but the elements are processed in reverse order. with:do: is used to iterative over corresponding elements of two equal-sized collections:
#(cut copy paste) with: #(#cut #copy #paste) do: [:label :selector | label = selectedLabel ifTrue: [^self perform: selector]]
Some indexable collections such as literal arrays are read-only and support only the <sequencedReadableCollection> protocol. Most indexable collections, however, can be written to and modified in place. The <sequencedReadableCollection> protocol (abbreviated as <sC>) is extended by the protocol <sequencedCollection> with the messages that can be used to modify the state of such collections:
Protocol <sequencedReadableCollection> abbreviated <sC> | Conforms to: <Collection> |
Messages: | |
at: <integer> put: <Object> ^ <Object> | Replaces the element at the specified index with the argument object |
atAll: <collection> put: <Object> ^ <sC> | Replaces all the element whose indices are specified by the argument collection with the argument object |
atAllPut: <Object> ^ <sC> | Replaces all elements of the receiver with the argument |
replaceFrom: <integer> to: <integer> with: <sRC> ^ <sC> | Replaces the identified subsequence of the receiver with the elements of the argument collection |
replaceFrom: <integer> to: <integer> with: <sRC> startingAt: <integer> ^ <sC> | Replaces the identified subsequence of the receiver with the elements of the argument collection starting at a specified index |
replaceFrom: <integer> to: <integer> withObject: <Object> ^ <sC> | Replaces each element of the identified subsequence of the receiver with the argument object |
We have already seen an example of the use of the at:put: message to insert elements into arrays. atAll:put: is used to store a single value at multiple indices, and atAllPut: stores a single value at all indices:
a := Array withAll: #(1 2 3 4 5 6). a atAll: #(2 4) put: inserted. Array(1 inserted 3 inserted 5 6) (String new: 5) atAllPut: $x xxxxx
The replace messages work much like the corresponding copyReplace message defined by <sequencedReadableCollection> except that instead of creating a new collection with substituted elements, they update the receiver collection in place.
Array and ByteArray are classes whose protocols conform to <sequencedCollection>. They differ only in the type of objects that may be stored as their elements. The elements of Array may be any object, whereas the elements of ByteArray are restricted to integer objects whose values are in the range of 0 to 255. Array literals are not modifiable, so they conform to <sequencedReadableCollection>. Interval is a class whose elements are numbers that form an arithmetic progression:
Protocol <Array> | Conforms to: <sequencedCollection> |
Previous | Table of Contents | Next |