3e-1 collections in squeak what are they? kinds of collections. basic operations. usage of...
TRANSCRIPT
![Page 1: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/1.jpg)
3E-1
Collections in SqueakCollections in Squeak
• What are they?
• Kinds of collections.
• Basic Operations.
• Usage of Inheritance in the Collections Library.
• Roman numbers example.
• The Stack Example: Defining a new kind of collection.
![Page 2: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/2.jpg)
3E-2
What are Collections?What are Collections?
• Collections provide the means for managing and manipulating groups of objects. Some kinds of collections: Set: represents an unordered group of objects. Elements
are added and removed by value. Bag: similar to set, but allows repetitions Dictionary: is also an unordered collection of elements,
but insertions and removals require an explicit key. Interval: represents a sequence of numbers in arithmetic
progression, either ascending or descending. List: is a group of objects having a specific linear
ordering. Insertions and removals are done in the extremes.
Array: a fixed-size collections. Elements can not be inserted or removed, but they may be overwritten.
String: can be considered to be a special form of Array, where the elements must be characters.
• Collections can be converted into a different kind by the use of messages like asSet, asArray, etc.
![Page 3: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/3.jpg)
3E-3
Classification of CollectionsClassification of Collections
• The different kinds of Collections may be classified according to several attributes.
• Size Fixed / Unbounded
• Ordering Ordered / Unordered
• Access Method By value / Indexed /Sequential/ key-based
• Mutability Mutable / Immutalbe
• Duplications policy
Accept / filter out
• Heterogeneous
Any kind of objects / one kind of objects
Choose the right Collection by examining its attributes.
![Page 4: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/4.jpg)
3E-4
4
Key Collection Classes in Squeak.Key Collection Classes in Squeak.
SetSet
SequenceableCollection*
SequenceableCollection*
OrderedCollectionOrderedCollectionArrayedCollection*ArrayedCollection*
Object*Object*
Collection*Collection*
IntervalInterval
StringString
DictionaryDictionary
ArrayArrayThis is only a partial view
BagBag
![Page 5: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/5.jpg)
3E-5
Some Collection MethodsSome Collection Methods
Are defined, redefined, optimized or forbidden (!) in subclasses
Accessingsize, capacity, at: anIndex, at: anIndex put: anElement
TestingisEmpty, includes: anElement, contains: aBlock, occurrencesOf: anElement
Adding add: anElement, addAll: aCollection
Removingremove: anElement, remove: anElement ifAbsent: aBlock, removeAll: aCollection
Enumeratingdo: aBlock, collect: aBlock, select: aBlock, reject: aBlock, detect: aBlock, detect: aBlock ifNone: aNoneBlock, inject: aValue into: aBinaryBlock
ConvertingasBag, asSet, asOrderedCollection, asSortedCollection, asArray,asSortedCollection: aBlock
Creationwith: anElement, with:with:, with:with:with:, with:with:with:with:, withAll: aCollection
![Page 6: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/6.jpg)
3E-6
CollectionsCollections’’ Attributes Attributes
Name Creation Fixed Order? Insertion Access Removal Method Size? Method Method Method
Set new no no add: includes: remove: with:
Dictionary new no no at:put: at: removeKey:
Interval n to: m yes yes none none none
Ordered- new no yes addFirst: first removeFirstCollection addLast: remove:Array new: yes yes at:put: at: none
with:
String new: yes yes at:put: at: noneBag new no no add: includes:
with:
![Page 7: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/7.jpg)
3E-7
Inserting an ElementInserting an Element• Indexed collections (Dictionary, Array) require an
explicit key and a value, by using the method at:put:D := Dictionary new.
D at:'father' put:'aba'; at:'mother' put:'ima';
at:'aunt' put:'doda'
a Dictionary('aunt'->'doda' 'father'->'aba' 'mother'->'ima' )
• Non-indexed collections require only a value, by using the method add:
S := Set new.
S add:'red'; add:'green'; add:'blue‘
a Set('green' 'blue‘' 'red')
• In OrderedCollection, values can be added in the beginning or end, using addFirst: and addLast:
L := OrderedCollection new.
L addLast: 'acharon'; addFirst: 'rishon'
an OrderedCollection('rishon' 'acharon')
![Page 8: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/8.jpg)
3E-8
Removing an ElementRemoving an Element
• In indexed collections the removal method requires the key.
D removeKey: 'aunt'
a Dictionary('father'->'aba‘ 'mother'->'ima' )
• In collections with fixed size (Array and String) elements can not be removed.
• In non-indexed collections the argument is the object to be removed.
S remove: 'green'
a Set ( 'blue' 'red' )
• In an OrderedCollection, an element can be removed from the beginning (removeFirst) or by value (remove:).
L removeFirst remove: 'acharon'
an OrderedCollection()
![Page 9: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/9.jpg)
3E-9
Accessing an ElementAccessing an Element• In indexed collections the elements are accessed by
key.'SmallTalk' at: 6
$T
• The method keys returns the keys of an indexed collection.
D keys
a Set ( 'father' 'mother' )
• In non-indexed collections we already have the value, hence the only question is whether it is in the collection.
S includes: 'black'
false
• The method includes: is defined for all collections.D keys includes: ‘father’
true
![Page 10: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/10.jpg)
3E-10
ConvertingConverting
Send asSet, asBag, asSortedCollection etc. to convert between kinds of collections
Send keys, values to extract collections from dictionaries
Use various factory methods to build new kinds of collections from old
Dictionary newFrom: {1->#a. 2->#b. 3->#c}
![Page 11: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/11.jpg)
3E-11
Selecting ElementsSelecting Elements• The method select: returns a collection containing all
the elements that satisfy some condition.
• It receives a one-argument block that is evaluated for each element in the collection, returning true or false.
• The returned collection is of the same class as the receiver in case it is Set, List, and Array, and Array otherwise.
#( 1 2 3 4 5 ) select: [ :i | ( i rem: 2 ) = 0 ]
#( 2 4 )
'1234567890' select: [ :c | c > $5 ]
'6789'
• The method reject: returns the complementary collection.
#( 1 2 3 4 5 ) asSet reject: [ :i | ( i rem: 2 ) = 0 ]
a Set ( 1 3 5 )
![Page 12: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/12.jpg)
3E-12
Performing ComputationsPerforming Computations• The method do: allows a computation to be performed
on every element in a collection.
• It also receives a one-argument block. B := [ :x | ( x rem: 2 ) = 0
ifTrue: [Transcript show: (x asString, ' is even!');cr]
ifFalse:[Transcript show: (x asString, ' is odd!');cr]].
#(1 2 3 4 5) do: B
1 is odd!
2 is even!
3 is odd!
4 is even!
5 is odd!
![Page 13: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/13.jpg)
3E-13
Collecting ResultsCollecting ResultsThe method collect: is similar to do:, but it produces a new collection containing the results of the block evaluation for each element of the receiver collection.
#( 1 2 3 4 5 ) collect: [ :i | i factorial ]
#( 1 2 6 24 120 )
#( 1 2 3 4 5 ) collect: [ :j | j rem: 2 ]
#( 1 0 1 0 1 )
D := Dictionary new.
D at:0 put:'even'; at:1 put:'odd‘.
#( 1 2 3 4 5 ) collect: [ :x | D at: ( x rem: 2 ) ]
#( 'odd' 'even' 'odd' 'even' 'odd' )
factor := 1.1.
grades := #(70 55 60 42) collect: [ :g | g * factor ]
#(77.0 60.5 66.0 46.2)
![Page 14: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/14.jpg)
3E-14
Accumulative ProcessingAccumulative Processing• The method inject:into: is useful for processing all
values of a collection and returning a single result.
• The first argument is the initial value, and the second is a two-parameter block that performs some computation.
• At each iteration the block receives the result of the previous computation and the next value in the collection.
A := #(1 2 3 4 5).
( A inject:0 into: [:a :b| a + b ] ) / A size.
3 “average of the values in the array”
A inject:0 into: [:x :y| x > y ifTrue:[x] ifFalse:[y]]
5 “maximum value in the array”
A inject:0 into: [:i :j| ( j rem: 2 ) = 0
ifTrue: [ i + 1 ] ifFalse: [ i ] ]
2 “number of even values in the array”
![Page 15: 3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example](https://reader036.vdocument.in/reader036/viewer/2022083007/56649e955503460f94b9990e/html5/thumbnails/15.jpg)
3E-15
Implementation ExamplesImplementation ExamplesTaken from Little Smalltalk, could be applied in SqueakTaken from Little Smalltalk, could be applied in Squeak
• Collection inject:into: inject: aValue into: aBlock | last |
last <- aValue.
self do: [:x | last <- aBlock value:last value:x ].
^last
• Collection size size
^self inject: 0 into: [ :x :y | x + 1 ]
• Collection occurrencesOf: occurrencesOf: anObject
^self inject: 0
into: [ :x :y | ( y = anObject )
ifTrue: [ x + 1 ]
ifFalse: [ x ] ]