user.websites.cs2390sum97.lectures.datastruct.master
P | N
- Pre-Defined Data Structures in Smalltalk
- Collections hierarchy
- Operations on collections
- Differences between kinds of collections
- Collections Hierarchy
- Operations on Collections
- They all understand
- -size
- -adding new elements (add:, addAll:, with:with:)
- -removing new elements (remove:, remove:ifAbsent:)
- -testing for elements (includes:, isEmpty, occurencesOf:)
- -enumerating elements (do:, select:, reject:, collect:, detect:)
- Demonstrating do:
- | aString |
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- count := 0.
- aString do: [:letter | letter asLowercase == $a ifTrue: [count := count + 1.]] .
- count "--> 5"
- Differences between select:, reject:, and detect:
- | aString |
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- aString select: [:each | each isVowel].
- " --> 'oieieoaooeooeoeaioeiaaa' "
- aString reject: [:gobbledy | gobbledy isVowel].
- " --> 'Nw s th tm fr ll gd mn t cm t th d f thr blh blh blh'"
- aString detect: [:gobbledy | gobbledy isVowel].
- " --> $o"
- What collect: and inject: do
- | someNumbers |
- someNumbers := #(1 2 3 4 5 6 7).
- someNumbers collect: [:aNumber | aNumber + 1].
- "--> (2 3 4 5 6 7 8 )"
- someNumbers inject: 0 into: [:subTotal :nextNumber | subTotal + nextNumber].
- "--> 28"
- Other ways to use inject: (functional programming)
- | aString |
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- aString inject: 0
- into: [:count :nextElement |
- count + (nextElement asLowercase == $a ifTrue: [1] ifFalse: [0])]
- "--> 5"
- inject: is actually select: or reject: or...
- | aString |
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- "inject: as select:"
- aString inject: '' into: [:newString :nextCharacter |
- newString , (nextCharacter isVowel ifTrue: [nextCharacter asString] ifFalse: [''])].
- "--> 'oieieoaooeooeoeaioeiaaa'"
- Side note: Tokenizing
- "First parse into words"
- | aString |
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- aString findTokens: ' '.
- "--> OrderedCollection ('Now' 'is' 'the' 'time' 'for' 'all' 'good' 'men' 'to' 'come' 'to' 'the' 'aid' 'of' 'their' 'blah' 'blah' 'blah' )"
- Bags vs. Sets
- | aString someWords |
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- someWords := Bag new.
- (aString findTokens: ' ') do: [:aWord | someWords add: aWord].
- someWords
- "--> Bag ('blah' 'blah' 'blah' 'men' 'of' 'the' 'the' 'aid' 'their' 'all' 'to' 'to' 'time' 'Now' 'for' 'good' 'is' 'come' )"
- someWords := Set new.
- (aString findTokens: ' ') do: [:aWord | someWords add: aWord].
- someWords
- "--> Set ('blah' 'men' 'of' 'the' 'aid' 'their' 'all' 'to' 'time' 'Now' 'for' 'good' 'is' 'come' )"
- SequenceableCollection New Abilities
- atAll: aCollection put: anObject
- atAll: anObject
- first
- last
- indexOf: anElement
- indexOf: anElement ifAbsent: someBlock
- indexOfSubCollection: aSubCollection startingAt: anIndex
- replaceFrom: start to: stop with: replacementCollection
- , anotherSequenceableCollection
- copyFrom: start to: stop
- copyReplaceAll: oldSubCollection with: newsubCollection
- copyWith: newelement
- copyWithout: oldelement
- findFirst: aBlock
- findLast: aBlock
- with: anotherSequenceable do: aBlock
- Examples:
- | aString someWords|
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- someWords := aString findTokens: ' '.
- someWords indexOf: 'blah'.
- "--> 16"
- someWords replaceFrom: 1 to: 2 with: #('Then' 'was')
- "--> OrderedCollection ('Then' 'was' 'the' 'time' 'for' 'all' 'good' 'men' 'to' 'come' 'to' 'the' 'aid' 'of' 'their' 'blah' 'blah' 'blah' )"
- someWords copyReplaceAll: #('Now' 'is') with: #('Then' 'was' 'almost')
- "--> OrderedCollection ('Then' 'was' 'almost' 'the' 'time' 'for' 'all' 'good' 'men' 'to' 'come' 'to' 'the' 'aid' 'of' 'their' 'blah' 'blah' 'blah' )"
- OrderedCollection new methods
- after: oldObject
- before: oldObject
- add: blah after: oldblah
- addFirst:, addLast:
- removeFirst, removeLast
- SortedCollection
- | aString someWords|
- aString := 'Now is the time for all good men to come to the aid of their blah blah blah'.
- someWords := SortedCollection new.
- someWords sortBlock: [:one :two | one <= two].
- (aString findTokens: ' ') do: [:aWord | someWords add: aWord].
- someWords
- "--> SortedCollection ('aid' 'all' 'blah' 'blah' 'blah' 'come' 'for' 'good' 'is' 'men' 'Now' 'of' 'the' 'the' 'their' 'time' 'to' 'to' )"
- LinkedList
- addFirst: aLink, nextLink: aLink, removeFirst, removeLast
- (Like OrderedCollection, but faster.)
- Interval
- (10 to: 100 by: 6) do: [:i | count := count + i].
- Strings and Texts
- Texts are Strings that also have font and style
- Strings can do some cool things
- '#irst string' match: 'first string' "--> true"
- Symbols
- Symbols are strings that are guaranteed to be unique
- (stored in only one place).
- => Very fast lookup. (Don't have to walk the string, just check the location.)
- Symbols are used internally for method name lookup.
- Games with symbols
- Circle exampleOne.
- Circle perform: #exampleOne.
- a := #exampleOne.
- Circle perform: a
- b := Circle.
- a := #exampleOne.
- b perform: a
Previous | Next
Last modified at 7/24/97; 1:40:53 PM
Other Links of Interest
College of Computing | EduTech Institute | GVU Center
Mark Guzdial | Papers | CS 2390 Spring '97 Home Page | STABLE | MMC-CaMILE
Slide Master