Previous Table of Contents Next


This block will result in the elements being sorted in ascending order, as defined for them using the < message.

All collection objects share a fundamental set of messages, regardless of their class or how they were created. The protocol <collection> defines this set of messages:

Protocol <collection> Conforms to: <Object>
Messages:
asArray ^ <Array> Returns an Array containing the receiver’s contents
asBag ^ <Bag> Returns a Bag containing the receiver’s contents
asOrderedCollection ^ <OrderedCollection> Returns a OrderedCollection containing the receiver’s contents
asSet ^ <Set> Returns a Set containing the receiver’s contents
asSortedCollection ^ <SortedCollection> Returns a SortedCollection containing the receiver’s contents
asSortedCollection: <block2> ^<SortedCollection> Returns a SortedCollection containing the receiver’s contents, using the argument block to specify the sort order
collect: <block1> ^ <collection> Returns a new collection that is the result of applying the argument block to each element of the receiver
detect: <block1> ^ <Object> Returns an element of the receiver for which the argument block evaluates to true
detect: <block1> ifNone: <block0> ^ <Object> Returns an element of the receiver for which the first argument evaluates to true: if there are none, evaluates the second block
do: <block1> Evaluates the argument block for each element of the receiver
includes: <Object> ^ <boolean> Tests if any element of the receiver equals the argument
inject: <Object> into: <block2> ^ <Object> Evaluates the argument block using each element and the succeeding result; returns the final value
isEmpty ^ <boolean> Tests if the receiver contains no elements
notEmpty ^ <boolean> Performs an inverted test whether receiver contains no elements
occurencesOf: <Object> ^ <integer> Determines how many elements equal the argument
reject: <block1> ^ <collection> Returns a new collection containing the elements of the receiver for which the argument block evaluates to false
select: <block1> ^ <collection> Returns a new collection containing the elements of the receiver for which the argument block evaluates to true
size ^ <integer> Returns the number of elements in the receiver

The protocol <collection> defines three groups of messages: conversion messages, query messages, and mapping messages. The conversion messages all have selectors that begin with the characters as. They are used to create different types of collection that contains the same elements as the receiver of the message. They are also used to guarantee that a collection is of a particular type. For example, a method that is willing to accept any type of collection as an argument but that needs to use an array for its internal processing might begin like this:

  someComputationOn: aCollection
      |tempCollection|
      tempCollection := aCollection asArray.
      “the rest of the method uses”
      ...

The conversion messages do not necessarily create a new object. If the receiver of a conversion message is already an instance of the class that is the target of the conversion, the receiver may be returned as the value of the message. In the preceding example, if the object that is the value of aCollection is already an array, then that same object may be returned from asArray and assigned to tempCollection. If a program requires the creation of a new object, then it should either use the copy message or the withAll: class message instead of one of the conversion messages.

The message asSortedCollection: allows the specification of a sort block for the SortedCollection. This sort block contains the elements of the receiver collection; asSortedCollection uses the default sort block.

The query messages provide information about the contents of the collection. For example, size, isEmpty, and notEmpty provide information on the number of elements in a collection. The message includes: is used to determine whether a particular object is in a collection, and occurencesOf: is used to determine how many times a particular object is in a collection. These messages and most other messages that test for particular objects within collections use the message = to compare objects. For the purposes of these messages, any two objects that answer true when compared using = are considered equivalent:

  #( 1 1.0 1.000s) occurencesOf: 2/2
      3    “These different number objects are all = to 2/2”

Most of the mapping messages are discussed in section 4.4. One additional mapping message is inject:into:. This message evaluates a block for each element of a collection while permitting a value to be passed from the evaluation for one element to the evaluation for the next element. For example, this expression will return the total length of all the strings in the receiver collection:

  #(‘abc’ ‘a string’ ‘qwer’)
         inject: 0 into: [:subtotal :element | subtotal + element size]
           15

When this expression is executed, the block is first evaluated with 0 (the inject: argument) as its first argument and ‘abc’ as its second argument. The block computes the size of ‘abc’, adds it to 0, and returns 3 as the result of the block. 3 is then passed as the first argument of the second evaluation of the block, and ‘a string’ is passed as the second argument. This computes 11, which is returned from the block and passed to the next block evaluation, along with ‘qwer’. This time 15 is returned and because all the elements of the receiver have been exhausted, that value is returned as the result of the inject:into: message.


Previous Table of Contents Next