#REFACTORING by stefan
authorStefan Vogel <sv@exept.de>
Fri, 28 Apr 2017 15:06:12 +0200
changeset 21735 abe6a5d8dd92
parent 21734 28119755ced8
child 21736 fc05f985abf2
#REFACTORING by stefan class: Collection - lint cleanup added: #at:ifAbsent: removed: #removeAllKeys: -moved to KeydCollection comment/format in: #contents #flatDetect:ifNone: changed: #at:ifNilOrAbsentPut: #groupBy:having: #minMax #with:andDefault:do: class: Collection class changed: #combiningEach:withEach:using:
Collection.st
--- a/Collection.st	Fri Apr 28 15:05:11 2017 +0200
+++ b/Collection.st	Fri Apr 28 15:06:12 2017 +0200
@@ -116,10 +116,10 @@
             newEl := aTwoArgBlock value:e1 value:e2.
 
             useIndex ifTrue:[
-                newColl at:idx put:(aTwoArgBlock value:e1 value:e2).
+                newColl at:idx put:newEl.
                 idx := idx + 1.
             ] ifFalse:[
-                newColl add:(aTwoArgBlock value:e1 value:e2)
+                newColl add:newEl
             ].
         ]
     ].
@@ -128,9 +128,11 @@
     "
      Set combiningEach:#(1 2 3 4 5) withEach:(10 to:100 by:10) using:[:a :b | a * b] 
      Set combiningEach:#(1 2 3 4 5) withEach:#(1 2 3 4 5) using:[:a :b | a * b] 
+     Array combiningEach:#(1 2 3 4 5) withEach:#(1 2 3 4 5) using:[:a :b | a * b] 
     "
 
     "Created: / 25-08-2010 / 17:21:47 / cg"
+    "Modified (comment): / 28-04-2017 / 14:38:41 / stefan"
 !
 
 new:size withAll:element
@@ -422,7 +424,11 @@
 !
 
 contents
+    "I am the contents of the collection"
+
     ^ self
+
+    "Modified (comment): / 28-04-2017 / 14:32:04 / stefan"
 !
 
 difference: aCollection
@@ -471,21 +477,23 @@
      elements for which keyBlock returns the same results, and return those 
      collections allowed by selectBlock. "
 
-    |result key|
+    |result|
 
     result := OrderedDictionary new.
     self do:[:e | 
+        |resultColl  key|
+
         key := keyBlock value: e.
-        (result includesKey: key) ifFalse: [
-            result at: key put: OrderedCollection new
-        ].
-        (result at: key) add: e
+        resultColl := result at:key ifAbsentPut:[OrderedCollection new].
+        resultColl add: e
     ].
-    ^ result := result select: selectBlock
-
-    "
-     #(1 2 3 4 5 6 7 8 9) groupBy:[:e | e odd] having:[:a |true]  
-    "
+    ^ result select: selectBlock
+
+    "
+     #(1 2 3 4 5 6 7 8 9) groupBy:[:e | e odd] having:[:a | true]  
+    "
+
+    "Modified (format): / 28-04-2017 / 13:56:37 / stefan"
 !
 
 ifEmpty:alternativeValue
@@ -573,6 +581,22 @@
     "
 !
 
+at:aKey ifAbsent:absentBlock
+    "return the element at aKey if valid.
+     If the key is not present, return the result of evaluating
+     the exceptionblock.
+     NOTICE:
+        in ST-80, this message is only defined for Dictionaries,
+        however, having a common protocol with indexed collections
+        often simplifies things."
+
+    ^ [
+        self at:aKey
+    ] on:KeyNotFoundError do:absentBlock.
+
+    "Created: / 28-04-2017 / 14:25:58 / stefan"
+!
+
 at:aKey ifNilOrAbsentPut:valueBlock
     "try to fetch the element at aKey. If either the key is invalid (as in a Dictionary)
      or there is no element stored under that key (as in an Array), set the element 
@@ -581,7 +605,7 @@
 
     |val|
 
-    val := self at:aKey ifAbsent:[ self at:aKey put:valueBlock value ].
+    val := self at:aKey ifAbsent:[].
     val isNil ifTrue:[
         self at:aKey put:(val := valueBlock value).
     ].
@@ -604,6 +628,7 @@
     "
 
     "Created: / 24-08-2010 / 17:07:21 / cg"
+    "Modified: / 28-04-2017 / 14:09:43 / stefan"
 !
 
 atAll:indexCollection put:anObject
@@ -1300,14 +1325,6 @@
     "Created: / 05-08-2010 / 13:52:21 / cg"
 !
 
-removeAllKeys:aCollection
-    "remove all keys of the argument, aCollection from the receiver.
-     Raises an error, if some element-to-remove is not in the receiver.
-     Notice: only works for keyed collections, such as dictionaries."
-
-    aCollection do:[:element | self removeKey:element].
-!
-
 removeAllSuchThat:aBlock
     "Apply the condition to each element and remove it if the condition is true.  
      Return a collection of removed elements.
@@ -1752,13 +1769,6 @@
     ^ self asArrayOfType:FloatArray
 !
 
-asHalfFloatArray
-    "return a new HalfFloatArray with the collection's elements
-     (which must convert to 16bit half-floats)."
-
-    ^ self asArrayOfType:HalfFloatArray
-!
-
 asIdentitySet
     "return a new IdentitySet with the receiver collection's elements"
 
@@ -1798,14 +1808,6 @@
     "
 !
 
-asList
-    "return a new List with the receiver collection's elements"
-
-    ^ self addAllTo:(List new:self size)
-
-    "Created: 14.2.1997 / 16:25:23 / cg"
-!
-
 asLongIntegerArray
     "return a new LongIntegerArray with the collection's elements
      (which must convert to 64bit integers in the range 0..16rFFFFFFFFFFFFFFFF)."
@@ -1890,36 +1892,6 @@
     ^ self addAllTo:(OrderedSet new:self size)
 !
 
-asRunArray
-    "return a new RunArray with the collection's elements"
-
-    ^ RunArray from:self.
-
-"/    |runs lastElement occurrences|
-"/
-"/    runs := RunArray new.
-"/    occurrences := 0.
-"/    self do:[:each |
-"/        each == lastElement ifTrue:[
-"/            occurrences := occurrences + 1
-"/        ] ifFalse:[
-"/            runs add:lastElement withOccurrences:occurrences.
-"/            occurrences := 1.
-"/            lastElement := each
-"/        ].
-"/    ].
-"/    occurrences ~~ 0 ifTrue:[
-"/        runs add:lastElement withOccurrences:occurrences
-"/    ].
-"/    ^ runs
-
-    "
-     #(1 2 3 3 3 4 4 4 4 5 6 7) asRunArray 
-    "
-
-    "Modified: / 7.4.1998 / 09:50:54 / cg"
-!
-
 asSequenceableCollection
     "return myself as a SequenceableCollection.
      I am already a Collection, but not sequenceable."
@@ -1937,17 +1909,6 @@
     ^ self addAllTo:(Set new:self size)
 !
 
-asSharedCollection
-    "return a shared collection on the receiver.
-     This implements synchronized (i.e. mutually excluded) access to me.
-     Use this for safe access when multiple processes access me concurrently.
-     Notice that this is a general (possibly suboptimal) mechanism, which should
-     work with all collections. Look for specialized collections (SharedQueue), which are
-     tuned for this kind of operation."
-
-    ^ SharedCollection for:self.
-!
-
 asSignedByteArray
     "return a new ByteArray with the collection's elements
      (which must convert to 8bit integers in the range -128..127)."
@@ -3097,8 +3058,6 @@
      Return the value from exceptionValue if none found"
 
     self do:[:each |
-        |result|
-
         (each isNonByteCollection) ifTrue:[
             each flatDo:[:el | (aBlock value:el) ifTrue:[^ el]].
         ] ifFalse:[
@@ -3143,6 +3102,8 @@
           )
       ) flatDetect:[:el | el>15] ifNone:#none       
     "
+
+    "Modified (format): / 28-04-2017 / 13:48:29 / stefan"
 !
 
 flatDo:aBlock
@@ -3841,7 +3802,7 @@
     "
 !
 
-with:aCollection andDefault:defaultElement do:aTwoArgBlock
+with:aSequenceableCollection andDefault:defaultElement do:aTwoArgBlock
     "evaluate the argument, aBlock for successive elements from
      each the receiver and the argument, aSequenceableCollection.
      If the receiver has more elements than the argument, use defaultElement 
@@ -3852,27 +3813,25 @@
 
     |index  "{ Class: SmallInteger }"|
 
+    "this method is redefined in SequenceableCollection, so we know, 
+     that we are not sequenceable. Maybe aCollection is..."
+    aSequenceableCollection isSequenceable ifFalse:[
+        ^ self error:'neither collection is sequenceable'.
+    ].
+
     index := 1.
-    aCollection isSequenceable ifFalse:[
-        self isSequenceable ifFalse:[
-            ^ self error:'neither collection is sequenceable'.
-        ].
-        aCollection do:[:element |
-            aTwoArgBlock value:(self at:index ifAbsent:defaultElement) value:element.
-            index := index + 1
-        ]
-    ] ifTrue:[
-        self do:[:element |
-            aTwoArgBlock value:element value:(aCollection at:index ifAbsent:defaultElement).
-            index := index + 1
-        ]
-    ]
+    self do:[:eachElement |
+        aTwoArgBlock value:eachElement value:(aSequenceableCollection at:index ifAbsent:defaultElement).
+        index := index + 1
+    ].
 
     "
      (1 to:3) with:#(one two) andDefault:99 do:[:num :sym |
         Transcript showCR:(num->sym)
      ]
     "
+
+    "Modified (format): / 28-04-2017 / 12:53:20 / stefan"
 !
 
 with:aCollection collect:aTwoArgBlock
@@ -5185,7 +5144,6 @@
 
     |min max|
 
-    min := max := nil.
     self do:[:each |
         min isNil ifTrue:[
             min := max := each
@@ -5212,6 +5170,8 @@
      (-1 to:-15 by:-4) minMax  
      (0 to:15 by:4) minMax     
     "
+
+    "Modified: / 28-04-2017 / 14:13:11 / stefan"
 !
 
 nthLargest:n