Browse Source

SequenceableCollection >> collect: optimization

Nicolas Petton 10 years ago
parent
commit
8d12dc91e2
3 changed files with 34 additions and 2 deletions
  1. 11 0
      js/Kernel-Collections.deploy.js
  2. 17 1
      js/Kernel-Collections.js
  3. 6 1
      st/Kernel-Collections.st

+ 11 - 0
js/Kernel-Collections.deploy.js

@@ -1695,6 +1695,17 @@ return $1;
 messageSends: ["at:", "atRandom", "size"]}),
 smalltalk.SequenceableCollection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "collect:",
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return self.map(function(each) {return aBlock._value_(each)});
+return self}, function($ctx1) {$ctx1.fill(self,"collect:",{aBlock:aBlock},smalltalk.SequenceableCollection)})},
+messageSends: []}),
+smalltalk.SequenceableCollection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "copyFrom:to:",

+ 17 - 1
js/Kernel-Collections.js

@@ -708,7 +708,7 @@ return _st(_st(aBlock)._value_(each)).__eq(false);
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"reject:",{aBlock:aBlock},smalltalk.Collection)})},
 args: ["aBlock"],
-source: "reject: aBlock\x0a\x09^self select: [:each | (aBlock value: each) = false]",
+source: "reject: aBlock\x0a\x09^ self select: [ :each | (aBlock value: each) = false ]",
 messageSends: ["select:", "=", "value:"],
 referencedClasses: []
 }),
@@ -2246,6 +2246,22 @@ referencedClasses: []
 }),
 smalltalk.SequenceableCollection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "collect:",
+category: 'enumerating',
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return self.map(function(each) {return aBlock._value_(each)});
+return self}, function($ctx1) {$ctx1.fill(self,"collect:",{aBlock:aBlock},smalltalk.SequenceableCollection)})},
+args: ["aBlock"],
+source: "collect: aBlock\x0a\x09\x22Optimized version\x22\x0a\x09<return self.map(function(each) {return aBlock._value_(each)})>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.SequenceableCollection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "copyFrom:to:",

+ 6 - 1
st/Kernel-Collections.st

@@ -189,7 +189,7 @@ intersection: aCollection
 !
 
 reject: aBlock
-	^self select: [:each | (aBlock value: each) = false]
+	^ self select: [ :each | (aBlock value: each) = false ]
 !
 
 select: aBlock
@@ -864,6 +864,11 @@ shallowCopy
 
 !SequenceableCollection methodsFor: 'enumerating'!
 
+collect: aBlock
+	"Optimized version"
+	<return self.map(function(each) {return aBlock._value_(each)})>
+!
+
 detect: aBlock ifNone: anotherBlock
 	<
 		for(var i = 0; i < self.length; i++)