1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Collection subclass: #MyCollection
   instanceVariableNames: 'contents'

MyCollection >> add: element
   ^contents add: element

MyCollection >> do: aBlock
   contents do: [:each | aBlock value: each]

MyCollection >> remove: element ifAbsent: aBlock
   ^contents remove: element ifAbsent: [aBlock value]

MyCollection >> atRandom: aGenerator
   ^contents atRandom: aGenerator

MyCollection >> setContents: newContents
   contents := newContents

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "

MyCollection class >> new
   ^self new: 1

MyCollection class >> new: numElements
   ^super new setContents: (OrderedCollection new: numElements); yourself

MyCollection class >> newFrom: aCollection
   ^self withAll: aCollection

MyCollection class >> example
   | myCollection |
   myCollection := MyCollection newFrom: #(4 3 2 1).   "=> a MyCollection(4 3 2 1) "
   myCollection collect: [:each | each * 2].           "=> a MyCollection(8 6 4 2) "
   myCollection select: [:each | each even].           "=> a MyCollection(4 2) "
   myCollection max.                                   "=> 4 "
   myCollection * 2.                                   "=> a MyCollection(8 6 4 2) "
   myCollection atRandom                               "=> 3 "