--- a/Collection.st Wed Sep 20 19:18:58 2017 +0200
+++ b/Collection.st Wed Sep 20 19:52:57 2017 +0200
@@ -2452,22 +2452,25 @@
"Created: / 15-03-2017 / 18:19:03 / cg"
!
-collect:aBlock
+collect:aBlockOrSymbol
"for each element in the receiver, evaluate the argument, aBlock
and return a new collection with the results"
|newCollection|
newCollection := self speciesForCollecting new:self size.
- self do:[:element | newCollection add:(aBlock value:element)].
+ self do:[:element | newCollection add:(aBlockOrSymbol value:element)].
^ newCollection
"
#(1 2 3 4) asSet collect:[:n | n * 2]
- "
-!
-
-collect:aBlock as:aClass
+ #(1 2 3 4) asSet collect:#mul2
+ "
+
+ "Modified (comment): / 20-09-2017 / 19:39:22 / stefan"
+!
+
+collect:aBlockOrSymbol as:aClass
"like collect, but use an instance of aClass to collect the results.
Also avoids the need for an extra intermediate collection which is created with
the standard coding: 'self asXXXX collect:[...]"
@@ -2481,12 +2484,12 @@
newCollection isSequenceable ifTrue:[
idx := 1.
self do:[:el |
- newCollection at:idx put:(aBlock value:el).
+ newCollection at:idx put:(aBlockOrSymbol value:el).
idx := idx + 1.
].
] ifFalse:[
self do:[:el |
- newCollection add:(aBlock value:el).
+ newCollection add:(aBlockOrSymbol value:el).
].
].
^ newCollection
@@ -2494,7 +2497,11 @@
"
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection
'abcdef' collect:[:char | char digitValue] as:ByteArray
- "
+
+ 'abcdef' collect:#digitValue as:ByteArray
+ "
+
+ "Modified (comment): / 20-09-2017 / 18:02:15 / stefan"
!
collect:collectBlock thenDetect:detectBlock ifNone:exceptionalValue
@@ -2517,7 +2524,10 @@
"
( #(1 2 3 4) collect:[:e | e squared] ) detect:[:e| e odd] ifNone:0
#(1 2 3 4) collect:[:e | e squared] thenDetect:[:e| e odd] ifNone:0
- "
+ #(1 2 3 4) collect:#squared thenDetect:#odd ifNone:0
+ "
+
+ "Modified (comment): / 20-09-2017 / 18:18:33 / stefan"
!
collect:collectBlock thenDo:aBlock
@@ -2547,16 +2557,19 @@
"Modified: / 11-07-2010 / 17:03:16 / cg"
!
-collect:collectBlock thenSelect:selectBlock
+collect:collectBlockOrSymbol thenSelect:selectBlockOrSymbol
"combination of collect followed by select.
May be redefined by some subclasses for optimal performance
(avoiding the creation of intermediate garbage)"
- ^ (self collect:collectBlock) select:selectBlock
+ ^ (self collect:collectBlockOrSymbol) select:selectBlockOrSymbol
"
#(1 2 3 4 5 6 7) collect:[:i | i * 2] thenSelect:[:i | i < 10]
- "
+ #(1 2 3 4 5 6 7) collect:#mul2 thenSelect:#isPerfectSquare
+ "
+
+ "Modified (format): / 20-09-2017 / 18:07:52 / stefan"
!
collect:collectBlock thenSelect:selectBlock as:aCollectionClass
@@ -2675,16 +2688,19 @@
"
#(1 2 3 4 6 8 10) count:[:a | a even]
#(1 nil nil nil 2 3 nil 4 5) count:[:a | a isNil]
- "
-!
-
-detect:aBlock
+ #(1 nil nil nil 2 3 nil 4 5) count:#isNil
+ "
+
+ "Modified (comment): / 20-09-2017 / 19:26:48 / stefan"
+!
+
+detect:aBlockOrSmbol
"evaluate the argument, aBlock for each element in the receiver until
the block returns true; in this case return the element which caused
the true evaluation.
If none of the evaluations returns true, report an error"
- ^ self detect:aBlock ifNone:[self errorNotFound]
+ ^ self detect:aBlockOrSmbol ifNone:[self errorNotFound]
"
#(1 2 3 4) detect:[:n | n odd]
@@ -2693,6 +2709,8 @@
#(1 2 3 4) detect: #odd
#(2 4 6 8) detect: #odd
"
+
+ "Modified (format): / 20-09-2017 / 18:08:13 / stefan"
!
detect:generatorBlock forWhich:testBlock ifNone:exceptionValue
@@ -2720,24 +2738,27 @@
"
!
-detect:aOneArgBlock ifNone:exceptionValue
+detect:aOneArgBlockOrSymbol ifNone:exceptionValue
"evaluate the argument aOneArgBlock for each element in the receiver until
the block returns true; in this case return the element that caused the
true evaluation.
If none of the evaluations returns true, return the value from exceptionValue"
self do:[:each |
- (aOneArgBlock value:each) ifTrue:[^ each].
+ (aOneArgBlockOrSymbol value:each) ifTrue:[^ each].
].
^ exceptionValue value
"
#(1 2 3 4) detect:[:n | n odd] ifNone:['sorry']
#(2 4 6 8) detect:[:n | n odd] ifNone:['sorry']
+ #(1 2 3 4) detect:#odd ifNone:['sorry']
+ #(2 4 6 8) detect:#odd ifNone:['sorry']
"
"Modified: / 13-09-2006 / 11:17:42 / cg"
"Modified (comment): / 13-09-2017 / 11:34:47 / mawalch"
+ "Modified (format): / 20-09-2017 / 18:10:34 / stefan"
!
detect:checkBlock thenCompute:evalBlock
@@ -2750,11 +2771,17 @@
"
#((1 'one') (2 'two') (3 'three') (4 'four'))
- detect:[:pair | pair first odd] thenCompute:[:pair | pair second]
+ detect:[:pair | pair first odd] thenCompute:[:pair | pair second]
+
+ #((1 'one') (2 'two') (3 'three') (4 'four'))
+ detect:[:pair | pair first odd] thenCompute:#second
#( (2 'two') (4 'four'))
- detect:[:pair | pair first odd] thenCompute:[:pair | pair second]
- "
+ detect:[:pair | pair first odd] thenCompute:[:pair | pair second]
+
+ "
+
+ "Modified (comment): / 20-09-2017 / 18:13:15 / stefan"
!
detect:checkBlock thenCompute:evalBlock ifNone:exceptionValue
@@ -2792,8 +2819,11 @@
"
#(1 2 3 4) detectLast:[:n | n odd]
+ #(1 2 3 4) detectLast:#odd
#(2 4 6 8) detectLast:[:n | n odd]
"
+
+ "Modified (comment): / 20-09-2017 / 18:14:33 / stefan"
!
detectLast:aBlock ifNone:exceptionValue
@@ -2813,7 +2843,7 @@
"
!
-detectMax: aBlock
+detectMax: aBlockOrSymbol
"Evaluate aBlock with each of the receiver's elements as argument.
Answer the element for which aBlock evaluates to the highest magnitude.
If the receiver collection is empty, return nil.
@@ -2824,7 +2854,7 @@
self do: [:each |
| val |
- val := aBlock value: each.
+ val := aBlockOrSymbol value: each.
"Note that there is no way to get the first element
which works for all kinds of Collections.
Must therefore test every one (maxValue is nil for the first element)."
@@ -2840,9 +2870,10 @@
"
"Created: / 20-08-2011 / 21:34:49 / cg"
-!
-
-detectMin: aBlock
+ "Modified (format): / 20-09-2017 / 18:15:35 / stefan"
+!
+
+detectMin: aBlockOrSymbol
"Evaluate aBlock with each of the receiver's elements as argument.
Answer the element for which aBlock evaluates to the lowest number.
If the receiver collection is empty, return nil."
@@ -2852,7 +2883,7 @@
self do: [:each |
| val |
- val := aBlock value: each.
+ val := aBlockOrSymbol value: each.
"Note that there is no way to get the first element
which works for all kinds of Collections.
Must therefore test every one (minValue is nil for the first element)."
@@ -2868,6 +2899,7 @@
"
"Created: / 20-08-2011 / 21:35:13 / cg"
+ "Modified (format): / 20-09-2017 / 18:15:28 / stefan"
!
do:aBlock
@@ -3207,6 +3239,7 @@
"
(1 to:10) fold:[:sum :el| sum + el]
+ (1 to:10) fold:#+
(1 to:15) fold:[:x :y| '(', x printString, '+', y printString, ')']
(1 to:15) reduce:[:x :y| '(', x printString, '+', y printString, ')']
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, ' ', b]
@@ -3215,6 +3248,7 @@
"
"Created: / 14-09-2011 / 16:29:53 / cg"
+ "Modified (comment): / 20-09-2017 / 19:24:41 / stefan"
!
inject:thisValue into:binaryBlock
@@ -3234,15 +3268,21 @@
#(1 2 3 4) inject:0 into:[:accu :element | accu + element]
(1 to:10) inject:0 into:[:accu :element | accu + element]
+ (1 to:10) inject:0 into:#+
find the minimum:
|coll|
coll := #(1 99 -15 20 100).
coll inject:(coll first) into:[:minSoFar :element | minSoFar min:element]
- "
-
- "Modified: 23.4.1996 / 13:47:06 / cg"
+
+ |coll|
+ coll := #(1 99 -15 20 100).
+ coll inject:(coll first) into:#min:
+ "
+
+ "Modified: / 23-04-1996 / 13:47:06 / cg"
+ "Modified (comment): / 20-09-2017 / 19:26:06 / stefan"
!
injectAndCollect:thisValue into:binaryBlock
@@ -3427,11 +3467,19 @@
^ self collect:selectorOrBlock.
"
- #(1 2 3 4) map:#negated
- #(1 2 3 4) collect:#negated
+ #(1 2 3 4) map:#negated
+
+ Time millisecondsToRun:[
+ (1 to:10000000) map:#negated
+ ]
+
+ Time millisecondsToRun:[
+ (1 to:10000000) collect:#negated
+ ]
"
"Modified (comment): / 29-02-2012 / 11:53:51 / cg"
+ "Modified (comment): / 20-09-2017 / 19:38:45 / stefan"
!
map:selectorOrBlock with:arg
@@ -3576,9 +3624,12 @@
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, ' ', b]
#(10 1 2 3) reduce:[:el :diff | diff - el]
#(10 1 2 3) reduce:[:el :diff | diff + el]
+ #(10 1 2 3) reduce:#+
+ #(10 1 2 3) reduce:#max:
"
"Created: / 28-02-2012 / 21:16:33 / cg"
+ "Modified (comment): / 20-09-2017 / 19:29:14 / stefan"
!
reduceLeft:aTwoArgBlock
@@ -3586,9 +3637,11 @@
"
#(1 2 3 4 5) reduceLeft:[:sum :el | sum + el]
+ #(1 2 3 4 5) reduceLeft:#+
"
"Created: / 20-07-2011 / 01:01:03 / cg"
+ "Modified (comment): / 20-09-2017 / 19:24:11 / stefan"
!
reject:aBlock
@@ -3657,10 +3710,12 @@
"
#(1 2 3 4) select:[:e | e odd]
(1 to:10) select:[:e | e even]
+ (1 to:10) select:#even
"
"Modified: / 07-08-2010 / 16:26:40 / cg"
"Modified: / 20-01-2017 / 17:42:33 / stefan"
+ "Modified (comment): / 20-09-2017 / 19:22:25 / stefan"
!
select:aBlock as:aCollectionClass
@@ -3899,7 +3954,7 @@
"Modified (format): / 28-04-2017 / 12:53:20 / stefan"
!
-with:aCollection collect:aTwoArgBlock
+with:aCollection collect:aTwoArgBlockOrSymbol
"evaluate the argument, aBlock for successive elements from
each the receiver and the argument, aSequenceableCollection;
The second argument, aBlock must be a two-argument block, which is
@@ -3913,14 +3968,17 @@
newCollection := self speciesForAdding new.
self with:aCollection do:[:el1 :el2 |
- newCollection add:(aTwoArgBlock value:el1 value:el2).
+ newCollection add:(aTwoArgBlockOrSymbol value:el1 value:el2).
].
^ newCollection as:self species
"
(1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)]
#(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)]
- "
+ #(1 2 3) with:#(10 20 30) collect:#@
+ "
+
+ "Modified (comment): / 20-09-2017 / 18:20:56 / stefan"
!
with:aCollection conform:aTwoArgBlock
@@ -3939,10 +3997,14 @@
(1 to:3) with:#(1 2 3 4) conform:[:a :b | a = b] --- raises an error
(1 to:3) with:#(1 22 3) conform:[:a :b | a = b]
(1 to:3) with:#(1 2 3) conform:[:a :b | a = b]
+ (1 to:3) with:#(1 2 3) conform:#=
+ (1 to:3) with:#(1.0 2.0 3.0) conform:#=
+ (1 to:3) with:#(1.0 2.0 3.0) conform:#==
(1 to:3) with:#('1' '2' '3') conform:[:a :b | a asString = b asString]
"
"Modified (comment): / 19-02-2017 / 18:30:08 / cg"
+ "Modified (comment): / 20-09-2017 / 19:20:05 / stefan"
!
with:aCollection contains:aTwoArgBlock
@@ -3961,10 +4023,11 @@
(1 to:3) with:#(1 2 3 4) contains:[:a :b | a ~= b] --- raises an error
(1 to:3) with:#(1 22 3) contains:[:a :b | a ~= b]
(1 to:3) with:#(1 2 3) contains:[:a :b | a ~= b]
+ (1 to:3) with:#(1 2 4) contains:#~=
"
"Created: / 30-06-2011 / 12:37:41 / cg"
- "Modified (Format): / 30-06-2011 / 12:40:38 / cg"
+ "Modified (comment): / 20-09-2017 / 19:21:31 / stefan"
!
with:aCollection count:aTwoArgBlock
@@ -3986,7 +4049,10 @@
"
(1 to:3) with:#(1 3 3) count:[:n1 :n2 | n1 = n2]
- "
+ (1 to:3) with:#(1 3 3) count:#=
+ "
+
+ "Modified (comment): / 20-09-2017 / 19:21:59 / stefan"
!
with:aCollection do:aTwoArgBlock