Levente Uzonyi uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ul.799.mcz ==================== Summary ==================== Name: Collections-ul.799 Author: ul Time: 2 July 2018, 8:56:32.531434 pm UUID: b0bbc442-2e2b-4d95-8b44-c3d9c504eae3 Ancestors: Collections-ul.798 - use ifNil:ifNotNil: & friends instead of isNil ifTrue:, == nil ifTrue, etc =============== Diff against Collections-ul.798 =============== Item was changed: ----- Method: Collection>>detectMax: (in category 'enumerating') ----- detectMax: aBlock "Evaluate aBlock with each of the receiver's elements as the argument. Answer the element for which aBlock evaluates to the highest magnitude. If collection empty, return nil. This method might also be called elect:." | maxElement maxValue | self do: [:each | | val | + maxValue + ifNotNil: [ + (val := aBlock value: each) > maxValue + ifTrue: [ + maxElement := each. + maxValue := val ] ] + ifNil: [ + "first element" - maxValue == nil - ifFalse: [ - (val := aBlock value: each) > maxValue ifTrue: [ - maxElement := each. - maxValue := val]] - ifTrue: ["first element" maxElement := each. + maxValue := aBlock value: each ]. - maxValue := aBlock value: each]. "Note that there is no way to get the first element that works for all kinds of Collections. Must test every one."]. ^ maxElement! Item was changed: ----- Method: Collection>>detectMin: (in category 'enumerating') ----- detectMin: aBlock "Evaluate aBlock with each of the receiver's elements as the argument. Answer the element for which aBlock evaluates to the lowest number. If collection empty, return nil." | minElement minValue | self do: [:each | | val | + minValue + ifNotNil: [ + (val := aBlock value: each) < minValue + ifTrue: [ + minElement := each. + minValue := val ] ] + ifNil: [ + "first element" - minValue == nil - ifFalse: [ - (val := aBlock value: each) < minValue ifTrue: [ - minElement := each. - minValue := val]] - ifTrue: ["first element" minElement := each. + minValue := aBlock value: each ]. - minValue := aBlock value: each]. "Note that there is no way to get the first element that works for all kinds of Collections. Must test every one."]. ^ minElement! Item was changed: ----- Method: LimitingLineStreamWrapper>>next (in category 'accessing') ----- next "Provide character-based access" + position ifNil: [ ^nil ]. - position isNil ifTrue: [^nil]. position < line size ifTrue: [^line at: (position := position + 1)]. line := stream nextLine. self updatePosition. ^ Character cr! Item was changed: ----- Method: LinkedList>>removeLink:ifAbsent: (in category 'removing') ----- removeLink: aLink ifAbsent: aBlock "Remove aLink from the receiver. If it is not there, answer the result of evaluating aBlock." | tempLink | aLink == firstLink ifTrue: [firstLink := aLink nextLink. aLink == lastLink ifTrue: [lastLink := nil]] ifFalse: [tempLink := firstLink. + [tempLink ifNil: [ ^ aBlock value ]. - [tempLink == nil ifTrue: [^aBlock value]. tempLink nextLink == aLink] whileFalse: [tempLink := tempLink nextLink]. tempLink nextLink: aLink nextLink. aLink == lastLink ifTrue: [lastLink := tempLink]]. "Not nilling the link enables us to delete while iterating" "aLink nextLink: nil." ^aLink! Item was changed: ----- Method: PositionableStream>>nextWord (in category 'nonhomogeneous accessing') ----- nextWord "Answer the next two bytes from the receiver as an Integer." | high low | + high := self next ifNil: [ ^false ]. + low := self next ifNil: [ ^false ]. - high := self next. - high==nil ifTrue: [^false]. - low := self next. - low==nil ifTrue: [^false]. ^(high asInteger bitShift: 8) + low asInteger! Item was changed: ----- Method: RWBinaryOrTextStream>>next (in category 'accessing') ----- next + isBinary ifFalse: [ ^super next ]. + ^super next ifNotNil: [ :character | character asInteger ]! - | byte | - ^ isBinary - ifTrue: [byte := super next. - byte ifNil: [nil] ifNotNil: [byte asciiValue]] - ifFalse: [super next]. - ! Item was changed: ----- Method: Stream>>collect: (in category 'enumerating') ----- collect: block ^ Generator on: [:g | [self atEnd] whileFalse: [ + g yield: (self next ifNotNil: [:object | block value: object])]]! - g yield: (self next - ifNil: [nil] - ifNotNil: [:object | block value: object])]]! Item was changed: ----- Method: Stream>>readInto:startingAt:count: (in category 'accessing') ----- readInto: aCollection startingAt: startIndex count: n "Read n objects into the given collection. Return number of elements that have been read." | obj | 0 to: n - 1 do: [:i | + obj := self next ifNil: [ ^i ]. - (obj := self next) == nil ifTrue: [^i]. aCollection at: startIndex + i put: obj]. ^n! Item was changed: ----- Method: String>>correctAgainst:continuedFrom: (in category 'converting') ----- correctAgainst: wordList continuedFrom: oldCollection "Like correctAgainst:. Use when you want to correct against several lists, give nil as the first oldCollection, and nil as the last wordList." + ^self + correctAgainstEnumerator: ( + wordList ifNotNil: [ + [ :action | wordList do: action without: nil ] ]) + continuedFrom: oldCollection! - ^ wordList isNil - ifTrue: [ self correctAgainstEnumerator: nil - continuedFrom: oldCollection ] - ifFalse: [ self correctAgainstEnumerator: [ :action | wordList do: action without: nil] - continuedFrom: oldCollection ]! Item was changed: ----- Method: String>>correctAgainstDictionary:continuedFrom: (in category 'converting') ----- correctAgainstDictionary: wordDict continuedFrom: oldCollection "Like correctAgainst:continuedFrom:. Use when you want to correct against a dictionary." + ^self + correctAgainstEnumerator: ( + wordDict ifNotNil: [ + [ :action | wordDict keysDo: action ] ]) + continuedFrom: oldCollection! - ^ wordDict isNil - ifTrue: [ self correctAgainstEnumerator: nil - continuedFrom: oldCollection ] - ifFalse: [ self correctAgainstEnumerator: [ :action | wordDict keysDo: action ] - continuedFrom: oldCollection ]! Item was changed: ----- Method: String>>correctAgainstEnumerator:continuedFrom: (in category 'private') ----- correctAgainstEnumerator: wordBlock continuedFrom: oldCollection "The guts of correction, instead of a wordList, there is a block that should take another block and enumerate over some list with it." | choices results maxChoices scoreMin | scoreMin := self size // 2 min: 3. maxChoices := 10. + choices := oldCollection ifNil: [ + SortedCollection sortBlock: [ :x :y | x value > y value ] ]. + wordBlock + ifNil: [ + results := OrderedCollection new. - oldCollection isNil - ifTrue: [ choices := SortedCollection sortBlock: [ :x :y | x value > y value ] ] - ifFalse: [ choices := oldCollection ]. - wordBlock isNil - ifTrue: - [ results := OrderedCollection new. 1 to: (maxChoices min: choices size) do: [ :i | results add: (choices at: i) key ] ] + ifNotNil: [ + wordBlock + value: [ :word | + | score | + (score := self alike: word) >= scoreMin + ifTrue: [ + choices add: (Association key: word value: score). + choices size >= maxChoices + ifTrue: [ scoreMin := (choices at: maxChoices) value ] ] ]. - ifFalse: - [ wordBlock value: [ :word | | score | - (score := self alike: word) >= scoreMin ifTrue: - [ choices add: (Association key: word value: score). - (choices size >= maxChoices) ifTrue: [ scoreMin := (choices at: maxChoices) value] ] ]. results := choices ]. ^ results! Item was changed: ----- Method: String>>findTokens:escapedBy: (in category 'accessing') ----- + findTokens: delimiters escapedBy: quoteDelimiters - findTokens: delimiters escapedBy: quoteDelimiters "Answer a collection of Strings separated by the delimiters, where delimiters is a Character or collection of characters. Two delimiters in a row produce an empty string (compare this to #findTokens, which treats sequential delimiters as one). The characters in quoteDelimiters are treated as quote characters, such that any delimiter within a pair of matching quoteDelimiter characters is treated literally, rather than as a delimiter. The quoteDelimiter characters may be escaped within a quoted string. Two sequential quote characters within a quoted string are treated as a single character. This method is useful for parsing comma separated variable strings for spreadsheet import and export." | tokens rs activeEscapeCharacter ts char token delimiterChars quoteChars | + delimiterChars := (delimiters ifNil: [ '' ]) asString. + quoteChars := (quoteDelimiters ifNil: [ '' ]) asString. - delimiterChars := (delimiters isNil - ifTrue: [''] - ifFalse: [delimiters]) asString. - quoteChars := (quoteDelimiters isNil - ifTrue: [''] - ifFalse: [quoteDelimiters]) asString. tokens := OrderedCollection new. rs := ReadStream on: self. activeEscapeCharacter := nil. ts := WriteStream on: ''. + [ rs atEnd ] + whileFalse: [ + char := rs next. + activeEscapeCharacter + ifNil: [ + (quoteChars includes: char) + ifTrue: [ activeEscapeCharacter := char ] + ifFalse: [ + (delimiterChars includes: char) + ifTrue: [ + token := ts contents. - [rs atEnd] - whileFalse: [char := rs next. - activeEscapeCharacter isNil - ifTrue: [(quoteChars includes: char) - ifTrue: [activeEscapeCharacter := char] - ifFalse: [(delimiterChars includes: char) - ifTrue: [token := ts contents. tokens add: token. + ts := WriteStream on: '' ] + ifFalse: [ ts nextPut: char ] ] ] + ifNotNil: [ + char == activeEscapeCharacter + ifTrue: [ + rs peek == activeEscapeCharacter + ifTrue: [ ts nextPut: rs next ] + ifFalse: [ activeEscapeCharacter := nil ] ] + ifFalse: [ ts nextPut: char ] ] ]. - ts := WriteStream on: ''] - ifFalse: [ts nextPut: char]]] - ifFalse: [char == activeEscapeCharacter - ifTrue: [rs peek == activeEscapeCharacter - ifTrue: [ts nextPut: rs next] - ifFalse: [activeEscapeCharacter := nil]] - ifFalse: [ts nextPut: char]]]. token := ts contents. + (tokens isEmpty and: [ token isEmpty ]) + ifFalse: [ tokens add: token ]. - (tokens isEmpty and: [token isEmpty]) - ifFalse: [tokens add: token]. ^ tokens! |
Free forum by Nabble | Edit this page |