an overview of essential collections
TRANSCRIPT
![Page 1: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/1.jpg)
Learning Object-OrientedProgramming and Design with TDD
An Overview of EssentialCollectionsStéphane Ducassehttp://stephane.ducasse.free.fr
http://www.pharo.orgW2S08
![Page 2: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/2.jpg)
What You Will Learn
Some basic collections Essential API to program collections Difference between literal and dynamic arrays
W2S08 2 / 32
![Page 3: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/3.jpg)
Collection Common Attributes
Pharo has a rich hierarchy of collection Common API: size, do:, select:, includes:, collect:... First element is at index 1 Can contain any object
W2S08 3 / 32
![Page 4: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/4.jpg)
Most Common Collections
OrderedCollection (dynamically growing) Array (fixed size, direct access) Set (no duplicates) Dictionary (key-based, aka. maps)
W2S08 4 / 32
![Page 5: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/5.jpg)
Essential Collection In a Nutshell
W2S08 5 / 32
![Page 6: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/6.jpg)
Common API Overview
Common messages work on all collections
1. creation: with: anElt, with:with:, withAll: aCollection2. accessing: size, at: anIndex, at: anIndex put: anElt3. testing: isEmpty, includes: anElt, contains: aBlock,4. adding: add: anElement, addAll: aCollection5. removing: remove: anElt, remove: anElt ifAbsent: aBlock, removeAll: aCollection6. enumerating: do: aBlock, collect: aBlock, select: aBlock, reject: aBlock, detect:aBlock, ...
7. converting: asBag, asSet, asOrderedCollection, asSortedCollection, asArray
W2S08 6 / 32
![Page 7: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/7.jpg)
Variable Size Object Creation
Message new instantiates one object Message new: size creates an object specifying its size
Array new: 4> #(nil nil nil nil)
Array new: 2>>> #(nil nil)
(OrderedCollection new: 1000)
W2S08 7 / 32
![Page 8: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/8.jpg)
With Specific Elements
OrderedCollection withAll: #(7 7 3 13)>>> an OrderedCollection(7 7 3 13)
Set withAll: #(7 7 3 13)>>> a Set( 7 3 13)
Remember: no duplicate in Sets
W2S08 8 / 32
![Page 9: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/9.jpg)
Creation with Default Value
OrderedCollection new: 5 withAll: 'a'>>> an OrderedCollection('a' 'a' 'a' 'a' 'a')
W2S08 9 / 32
![Page 10: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/10.jpg)
First Element Starts At 1
#('Calvin' 'hates' 'Suzie') at: 2>>> 'hates'
#('Calvin' 'hates' 'Suzie') asOrderedCollection at: 2>>> 'hates'
W2S08 10 / 32
![Page 11: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/11.jpg)
Collections can be HeterogenousCollections can contain any sort of objects
#('calvin' (1 2 3))>>> #('calvin' #(1 2 3))
An array composed of a string and an array
| s |s := Set new.s add: Set new;add: 1;add: 2.
s asArray>>> an Array(1 2 a Set())
A set containing an empty set and some numbers
W2S08 11 / 32
![Page 12: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/12.jpg)
Iteration Using message do: aBlock But many iterators (see Iterators Lecture)
#('Calvin' 'hates' 'Suzie')do: [ :each | Transcript show: each ; cr ]
W2S08 12 / 32
![Page 13: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/13.jpg)
Arrays Fixed size collection Direct access: at: and at:put: Has literal syntax: #( ... ) Can also be created using new:
#('Calvin' 'hates' 'Suzie') size>>> 3
is equivalent to
((Array new: 3)at: 1 put: 'Calvin';at: 2 put: 'hates';at: 3 put: 'Suzie') size
>>> 3
W2S08 13 / 32
![Page 14: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/14.jpg)
Accessing Elements
Getting the size of a collection
#('Calvin' 'hates' 'Suzie') size>>> 3
Accessing the 2nd element using at: anIndex
#('Calvin' 'hates' 'Suzie') at: 2>>> 'hates'
Remember collection index starts at 1
W2S08 14 / 32
![Page 15: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/15.jpg)
Accessing Out of Bounds Elements
#('Calvin' 'hates' 'Suzie') at: 55>>> SubscriptOutOfBounds Error
W2S08 15 / 32
![Page 16: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/16.jpg)
Modifying Elements
Use the message at: anIndex put: anObjectModifying the second element of the receiver
#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'>>> #('Calvin' 'loves' 'Suzie')
W2S08 16 / 32
![Page 17: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/17.jpg)
Literal Arrays
Literal arrays contain objects that have a textual (literal) representation: numbers,strings, nil, symbols
#(45 'milou' 1300 true #tintin)>>> #(45 'milou' 1300 true #tintin)
They are instances of the class Array
#(45 38 1300 8) class>>> Array
W2S08 17 / 32
![Page 18: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/18.jpg)
Literals Arrays are Array Instances
Literal arrays are equivalent to a dynamic versionA literal array
#(45 38 'milou' 8)>>> #(45 38 'milou' 8)
An array
Array with: 45 with: 38 with: 'milou'with: 8>>> #(45 38 'milou' 8)
W2S08 18 / 32
![Page 19: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/19.jpg)
OrderedCollection
Sequenceable Growing size add:, remove:
| ordCol |ordCol := OrderedCollection new.ordCol add: 'Reef'; add: 'Pharo'; addFirst: 'Pharo'.ordCol>>> an OrderedCollection('Pharo' 'Reef' 'Pharo')ordCol add: 'Seaside'.ordCol>>> an OrderedCollection('Pharo' 'Reef' 'Pharo' 'Seaside')
W2S08 19 / 32
![Page 20: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/20.jpg)
Conversion
#('Pharo' 'Reef' 'Pharo' 'Pharo') asOrderedCollection>>> an OrderedCollection('Pharo' 'Reef' 'Pharo' 'Pharo')
W2S08 20 / 32
![Page 21: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/21.jpg)
Set
No duplicates Growing size add:, remove: Can contain any object including other Sets
#('Pharo' 'Reef' 'Pharo' 'Pharo') asSet>>> a Set('Pharo' 'Reef')
Set with: (Set with: 1) with: (Set with: 2)>>> a Set(a Set(1) aSet(2))
W2S08 21 / 32
![Page 22: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/22.jpg)
Conversion
Collections can be converted simply to other collections
asOrderedCollection
asSet
asArray
W2S08 22 / 32
![Page 23: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/23.jpg)
Dictionary
Key/values Growing size Accessing at:, at:ifAbsent: Changing/adding at:put:, at:ifAbsentPut: Iterating: do:, keysDo:, keysAndValuesDo:
W2S08 23 / 32
![Page 24: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/24.jpg)
Dictionary Creation
| days |days := Dictionary new.daysat: #January put: 31;at: #February put: 28;at: #March put: 31.
W2S08 24 / 32
![Page 25: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/25.jpg)
Alternate Dictionary Creation
| days |days := Dictionary new.daysat: #January put: 31;at: #February put: 28;at: #March put: 31.
is equivalent to
{ #January−> 31.#February−> 28.#March−> 31} asDictionary
W2S08 25 / 32
![Page 26: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/26.jpg)
Pairs
(#January−> 31) key>>> #January
(#January−> 31) value>>> 31
W2S08 26 / 32
![Page 27: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/27.jpg)
Dictionary Access
| days |days := Dictionary new.daysat: #January put: 31;at: #February put: 28;at: #March put: 31.
days at: #January>>> 31
days at: #NoMonth>>> KeyNotFound Error
days at: #NoMonth ifAbsent: [0]>>> 0
W2S08 27 / 32
![Page 28: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/28.jpg)
Dictionary Iteration
days do: [ :each | Transcript show: each ;cr ]
prints
312831
Why? Because
Dictionary >> do: aBlock
^ self valuesDo: aBlock
W2S08 28 / 32
![Page 29: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/29.jpg)
Keys and Values Iteration
days keysAndValuesDo:[ :k :v | Transcript show: k asString, ' has ', v printString, ' days' ; cr ]
shows:
January has 31 daysFebruary has 28 daysMarch has 31 days
W2S08 29 / 32
![Page 30: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/30.jpg)
Summary
Easy to use collections Common vocabulary Simple conversion between them Easy to discover!
W2S08 30 / 32
![Page 31: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/31.jpg)
Resources
Pharo Mooc - W3S07 Videos http://mooc.pharo.org Pharo by Example http://books.pharo.org
W2S08 31 / 32
![Page 32: An Overview of Essential Collections](https://reader031.vdocuments.mx/reader031/viewer/2022020701/61f65da6ee27eb06583b8210/html5/thumbnails/32.jpg)
A course by Stéphane Ducassehttp://stephane.ducasse.free.fr
Reusing some parts of the Pharo Mooc by
Damien Cassou, Stéphane Ducasse, Luc Fabressehttp://mooc.pharo.org
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 Francehttps://creativecommons.org/licenses/by-nc-nd/3.0/fr/