1 / 15

Collections in Squeak

Collections 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. What are Collections?.

gianna
Download Presentation

Collections in Squeak

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collections 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.

  2. 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.

  3. Classification 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.

  4. Key Collection Classes in Squeak. Object* Collection* Set Bag SequenceableCollection* Dictionary ArrayedCollection* Interval OrderedCollection This is only a partial view Array String

  5. Some Collection Methods Are defined, redefined, optimized or forbidden (!) in subclasses

  6. Collections’ 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: none Bag new no no add: includes: with:

  7. Inserting 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')

  8. Removing 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()

  9. Accessing 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

  10. Converting • 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}

  11. Selecting 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 )

  12. Performing 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!

  13. Collecting Results The 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)

  14. Accumulative 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”

  15. Implementation ExamplesTaken 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 ] ]

More Related