An Overview of Essential Collections
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
What You Will Learn
- Some basic collections
- Essential API to program collections
- Difference between literal and dynamic arrays
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
Most Common Collections
OrderedCollection
(dynamically growing)
Array
(fixed size, direct access)
Set
(no duplicates)
Dictionary
(key-based, aka. maps)
Essential Collection In a Nutshell
Common API Overview
Common messages work on all collections
- creation:
with: anElt
, with:with:
, withAll: aCollection
- accessing:
size
, at: anIndex
, at: anIndex put: anElt
- testing:
isEmpty
, includes: anElt
, contains: aBlock
,
- adding:
add: anElement
, addAll: aCollection
- removing:
remove: anElt
, remove: anElt ifAbsent: aBlock
, removeAll: aCollection
- enumerating:
do: aBlock
, collect: aBlock
, select: aBlock
, reject: aBlock
, detect: aBlock
, ...
- converting:
asBag
, asSet
, asOrderedCollection
, asSortedCollection
, asArray
Variable Size Object Creation
- Message
new
instantiates one object
- Message
new: size
creates an object specifying its size
With Specific Elements
Remember: no duplicate in Sets
Creation with Default Value
First Element Starts At 1
Collections can be Heterogenous
Collections can contain any sort of objects
- An array composed of a string and an array
- A set containing an empty set and some numbers
Iteration
- Using message
do: aBlock
- But many iterators (see Iterators Lecture)
Arrays
- Fixed size collection
- Direct access:
at:
and at:put:
- Has literal syntax:
#( ... )
- Can also be created using
new:
is equivalent to
Accessing Elements
Getting the size of a collection
Accessing the 2nd element using at: anIndex
Remember collection index starts at 1
Accessing Out of Bounds Elements
Modifying Elements
Use the message at: anIndex put: anObject
Modifying the second element of the receiver
Literal Arrays
Literal arrays contain objects that have a textual (literal) representation: numbers, strings, nil, symbols
They are instances of the class Array
Literals Arrays are Array Instances
Literal arrays are equivalent to a dynamic version
A literal array
An array
OrderedCollection
- Sequenceable
- Growing size
add:
, remove:
Conversion
Set
- No duplicates
- Growing size
add:
, remove:
- Can contain any object including other Sets
Conversion
Collections can be converted simply to other collections
Dictionary
- Key/values
- Growing size
- Accessing
at:
, at:ifAbsent:
- Changing/adding
at:put:
, at:ifAbsentPut:
- Iterating:
do:
, keysDo:
, keysAndValuesDo:
Dictionary Creation
Alternate Dictionary Creation
is equivalent to
Pairs
Dictionary Access
Dictionary Iteration
prints
Why?
Because
Keys and Values Iteration
shows:
Summary
- Easy to use collections
- Common vocabulary
- Simple conversion between them
- Easy to discover!
/