Create the RevertingCollection class as a subclass of SequeanceableCollection with one collection instance variable. Now define these three methods (instance side):
on: aCollection collection := aCollection size ^collection size at: index ^collection at: self size - index + 1
Done. Now you can do the following:
stream := (RevertingCollection new on: #(1 2 3)) readStream.
and you will get
stream next "3". stream next "2". stream next "1"
You can go ahead and implement the message
SequenceableCollection >>
So it all comes down to just
ADDITION
As indicated in the comments, there are no two parts that are:
1. Instance creation method (class)
RevertingCollection class >> #on: aCollection ^self new on: aCollection
With this addition, the above method should be rewritten as follows:
SequenceableCollection >>
Note. Other small traders would prefer this method to be called #withAll:
2. The following copy method:
RevertingCollection >>
This method is required to support #next: in the reverse read stream.
source share