Browse Source

Merge pull request #355 from herby/gh-354

Fixes #354.
Nicolas Petton 12 years ago
parent
commit
2b984d3c9e

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

@@ -3366,6 +3366,19 @@ return $1;
 }),
 smalltalk.Set);
 
+smalltalk.addMethod(
+"_collect_",
+smalltalk.method({
+selector: "collect:",
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(_st(self)._class())._withAll_(_st(self["@elements"])._collect_(aBlock));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"collect:",{aBlock:aBlock}, smalltalk.Set)})}
+}),
+smalltalk.Set);
+
 smalltalk.addMethod(
 "_detect_ifNone_",
 smalltalk.method({

+ 18 - 0
js/Kernel-Collections.js

@@ -4542,6 +4542,24 @@ referencedClasses: []
 }),
 smalltalk.Set);
 
+smalltalk.addMethod(
+"_collect_",
+smalltalk.method({
+selector: "collect:",
+category: 'enumerating',
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(_st(self)._class())._withAll_(_st(self["@elements"])._collect_(aBlock));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"collect:",{aBlock:aBlock}, smalltalk.Set)})},
+args: ["aBlock"],
+source: "collect: aBlock\x0a\x09^self class withAll: (elements collect: aBlock)",
+messageSends: ["withAll:", "collect:", "class"],
+referencedClasses: []
+}),
+smalltalk.Set);
+
 smalltalk.addMethod(
 "_detect_ifNone_",
 smalltalk.method({

+ 13 - 0
js/Kernel-Tests.deploy.js

@@ -2881,6 +2881,19 @@ return self}, function($ctx1) {$ctx1.fill(self,"testAt",{}, smalltalk.SetTest)})
 }),
 smalltalk.SetTest);
 
+smalltalk.addMethod(
+"_testCollect",
+smalltalk.method({
+selector: "testCollect",
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._assert_equals_([(0), (2)],_st(_st(_st(_st([(5), (6), (8)])._asSet())._collect_((function(x){
+return smalltalk.withContext(function($ctx2) {
return _st(x).__backslash_backslash((3));
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})))._asArray())._sorted());
+return self}, function($ctx1) {$ctx1.fill(self,"testCollect",{}, smalltalk.SetTest)})}
+}),
+smalltalk.SetTest);
+
 smalltalk.addMethod(
 "_testPrintString",
 smalltalk.method({

+ 18 - 0
js/Kernel-Tests.js

@@ -3712,6 +3712,24 @@ referencedClasses: ["Set", "Error"]
 }),
 smalltalk.SetTest);
 
+smalltalk.addMethod(
+"_testCollect",
+smalltalk.method({
+selector: "testCollect",
+category: 'tests',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._assert_equals_([(0), (2)],_st(_st(_st(_st([(5), (6), (8)])._asSet())._collect_((function(x){
+return smalltalk.withContext(function($ctx2) {
return _st(x).__backslash_backslash((3));
+}, function($ctx2) {$ctx2.fillBlock({x:x},$ctx1)})})))._asArray())._sorted());
+return self}, function($ctx1) {$ctx1.fill(self,"testCollect",{}, smalltalk.SetTest)})},
+args: [],
+source: "testCollect\x0a\x09self assert: #(0 2) equals: (#(5 6 8) asSet collect: [ :x | x \x5c\x5c 3 ]) asArray sorted",
+messageSends: ["assert:equals:", "sorted", "asArray", "collect:", "\x5c\x5c", "asSet"],
+referencedClasses: []
+}),
+smalltalk.SetTest);
+
 smalltalk.addMethod(
 "_testPrintString",
 smalltalk.method({

+ 4 - 0
st/Kernel-Collections.st

@@ -1457,6 +1457,10 @@ asArray
 
 !Set methodsFor: 'enumerating'!
 
+collect: aBlock
+	^self class withAll: (elements collect: aBlock)
+!
+
 detect: aBlock ifNone: anotherBlock
 	^elements detect: aBlock ifNone: anotherBlock
 !

+ 4 - 0
st/Kernel-Tests.st

@@ -1480,6 +1480,10 @@ testAt
 	self should: [Set new at: 1 put: 2] raise: Error
 !
 
+testCollect
+	self assert: #(0 2) equals: (#(5 6 8) asSet collect: [ :x | x \\ 3 ]) asArray sorted
+!
+
 testPrintString
 	| set |
 	set := Set new.