Browse Source

Add missing implementations of #select:thenCollect:.

Herbert Vojčík 7 years ago
parent
commit
c5dd89fe09
4 changed files with 207 additions and 0 deletions
  1. 82 0
      src/Kernel-Collections.js
  2. 17 0
      src/Kernel-Collections.st
  3. 101 0
      src/Kernel-Tests.js
  4. 7 0
      src/Kernel-Tests.st

+ 82 - 0
src/Kernel-Collections.js

@@ -2961,6 +2961,47 @@ messageSends: ["new", "class", "keysAndValuesDo:", "ifTrue:", "value:", "at:put:
 }),
 $globals.AssociativeCollection);
 
+$core.addMethod(
+$core.method({
+selector: "select:thenCollect:",
+protocol: "enumerating",
+fn: function (selectBlock,collectBlock){
+var self=this;
+var newDict;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1;
+newDict=$recv(self._class())._new();
+self._keysAndValuesDo_((function(key,value){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+$1=$recv(selectBlock)._value_(value);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx2.sendIdx["value:"]=1;
+//>>excludeEnd("ctx");
+if($core.assert($1)){
+return $recv(newDict)._at_put_(key,$recv(collectBlock)._value_(value));
+}
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({key:key,value:value},$ctx1,1)});
+//>>excludeEnd("ctx");
+}));
+return newDict;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"select:thenCollect:",{selectBlock:selectBlock,collectBlock:collectBlock,newDict:newDict},$globals.AssociativeCollection)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["selectBlock", "collectBlock"],
+source: "select: selectBlock thenCollect: collectBlock\x0a\x09| newDict |\x0a\x09newDict := self class new.\x0a\x09self keysAndValuesDo: [ :key :value |\x0a\x09\x09(selectBlock value: value) ifTrue: [ newDict at: key put: (collectBlock value: value) ]].\x0a\x09^ newDict",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["new", "class", "keysAndValuesDo:", "ifTrue:", "value:", "at:put:"]
+}),
+$globals.AssociativeCollection);
+
 $core.addMethod(
 $core.method({
 selector: "shallowCopy",
@@ -8603,6 +8644,47 @@ messageSends: ["new", "class", "do:", "ifTrue:", "value:", "add:"]
 }),
 $globals.Set);
 
+$core.addMethod(
+$core.method({
+selector: "select:thenCollect:",
+protocol: "enumerating",
+fn: function (selectBlock,collectBlock){
+var self=this;
+var collection;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1;
+collection=$recv(self._class())._new();
+self._do_((function(each){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+$1=$recv(selectBlock)._value_(each);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx2.sendIdx["value:"]=1;
+//>>excludeEnd("ctx");
+if($core.assert($1)){
+return $recv(collection)._add_($recv(collectBlock)._value_(each));
+}
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,1)});
+//>>excludeEnd("ctx");
+}));
+return collection;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"select:thenCollect:",{selectBlock:selectBlock,collectBlock:collectBlock,collection:collection},$globals.Set)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["selectBlock", "collectBlock"],
+source: "select: selectBlock thenCollect: collectBlock\x0a\x09| collection |\x0a\x09collection := self class new.\x0a\x09self do: [ :each |\x0a\x09\x09(selectBlock value: each) ifTrue: [\x0a\x09\x09\x09collection add: (collectBlock value: each) ] ].\x0a\x09^ collection",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["new", "class", "do:", "ifTrue:", "value:", "add:"]
+}),
+$globals.Set);
+
 $core.addMethod(
 $core.method({
 selector: "size",

+ 17 - 0
src/Kernel-Collections.st

@@ -712,6 +712,14 @@ select: aBlock
 	^ newDict
 !
 
+select: selectBlock thenCollect: collectBlock
+	| newDict |
+	newDict := self class new.
+	self keysAndValuesDo: [ :key :value |
+		(selectBlock value: value) ifTrue: [ newDict at: key put: (collectBlock value: value) ]].
+	^ newDict
+!
+
 valuesDo: aBlock
 	self subclassResponsibility
 !
@@ -1983,6 +1991,15 @@ select: aBlock
 		(aBlock value: each) ifTrue: [
 			collection add: each ] ].
 	^ collection
+!
+
+select: selectBlock thenCollect: collectBlock
+	| collection |
+	collection := self class new.
+	self do: [ :each |
+		(selectBlock value: each) ifTrue: [
+			collection add: (collectBlock value: each) ] ].
+	^ collection
 ! !
 
 !Set methodsFor: 'initialization'!

+ 101 - 0
src/Kernel-Tests.js

@@ -4601,6 +4601,107 @@ messageSends: ["assert:equals:", "select:", "collection", "new", "collectionClas
 }),
 $globals.CollectionTest);
 
+$core.addMethod(
+$core.method({
+selector: "testSelectThenCollect",
+protocol: "tests",
+fn: function (){
+var self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $2,$1,$4,$3,$6,$5,$7,$9,$8;
+$2=self._collection();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["collection"]=1;
+//>>excludeEnd("ctx");
+$1=$recv($2)._select_thenCollect_((function(){
+return false;
+
+}),"isString");
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["select:thenCollect:"]=1;
+//>>excludeEnd("ctx");
+$4=self._collectionClass();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["collectionClass"]=1;
+//>>excludeEnd("ctx");
+$3=$recv($4)._new();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["new"]=1;
+//>>excludeEnd("ctx");
+self._assert_equals_($1,$3);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=1;
+//>>excludeEnd("ctx");
+$6=self._collection();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["collection"]=2;
+//>>excludeEnd("ctx");
+$5=$recv($6)._select_thenCollect_((function(){
+return true;
+
+}),(function(x){
+return x;
+
+}));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["select:thenCollect:"]=2;
+//>>excludeEnd("ctx");
+$7=self._collection();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["collection"]=3;
+//>>excludeEnd("ctx");
+self._assert_equals_($5,$7);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=2;
+//>>excludeEnd("ctx");
+$8=$recv(self._collection())._select_thenCollect_((function(each){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+$9=self._sampleNewValue();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx2.sendIdx["sampleNewValue"]=1;
+//>>excludeEnd("ctx");
+return $recv(each).__eq($9);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,4)});
+//>>excludeEnd("ctx");
+}),(function(x){
+return x;
+
+}));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["select:thenCollect:"]=3;
+//>>excludeEnd("ctx");
+self._assert_equals_($8,$recv(self._collectionClass())._new());
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx["assert:equals:"]=3;
+//>>excludeEnd("ctx");
+self._assert_equals_($recv(self._collectionWithNewValue())._select_thenCollect_((function(each){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+return $recv(each).__tild_eq(self._sampleNewValue());
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,6)});
+//>>excludeEnd("ctx");
+}),"printString"),self._collectionOfPrintStrings());
+return self;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"testSelectThenCollect",{},$globals.CollectionTest)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: [],
+source: "testSelectThenCollect\x0a\x09self assert: (self collection select: [ false ] thenCollect: #isString) equals: self collectionClass new.\x0a\x09self assert: (self collection select: [ true ] thenCollect: [:x|x]) equals: self collection.\x0a\x09self assert: (self collection select: [ :each | each = self sampleNewValue ] thenCollect: [:x|x]) equals: self collectionClass new.\x0a\x09self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ] thenCollect: #printString) equals: self collectionOfPrintStrings",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["assert:equals:", "select:thenCollect:", "collection", "new", "collectionClass", "=", "sampleNewValue", "collectionWithNewValue", "~=", "collectionOfPrintStrings"]
+}),
+$globals.CollectionTest);
+
 $core.addMethod(
 $core.method({
 selector: "testSingle",

+ 7 - 0
src/Kernel-Tests.st

@@ -779,6 +779,13 @@ testSelect
 	self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ]) equals: self collection
 !
 
+testSelectThenCollect
+	self assert: (self collection select: [ false ] thenCollect: #isString) equals: self collectionClass new.
+	self assert: (self collection select: [ true ] thenCollect: [:x|x]) equals: self collection.
+	self assert: (self collection select: [ :each | each = self sampleNewValue ] thenCollect: [:x|x]) equals: self collectionClass new.
+	self assert: (self collectionWithNewValue select: [ :each | each ~= self sampleNewValue ] thenCollect: #printString) equals: self collectionOfPrintStrings
+!
+
 testSingle
 	self should: [ self collectionClass new single ] raise: Error.
 	self should: [ self collection single ] raise: Error.