Parcourir la source

combined ifEmpty:ifNotEmpty: and vice versa

Herbert Vojčík il y a 10 ans
Parent
commit
fc6c75521f
4 fichiers modifiés avec 122 ajouts et 6 suppressions
  1. 48 0
      js/Kernel-Collections.js
  2. 56 6
      js/Kernel-Tests.js
  3. 12 0
      st/Kernel-Collections.st
  4. 6 0
      st/Kernel-Tests.st

+ 48 - 0
js/Kernel-Collections.js

@@ -519,6 +519,30 @@ referencedClasses: []
 }),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "ifEmpty:ifNotEmpty:",
+category: 'testing',
+fn: function (aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1;
+$2=self._isEmpty();
+if(smalltalk.assert($2)){
+$1=_st(aBlock)._value();
+$ctx1.sendIdx["value"]=1;
+} else {
+$1=_st(anotherBlock)._value();
+};
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"ifEmpty:ifNotEmpty:",{aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.Collection)})},
+args: ["aBlock", "anotherBlock"],
+source: "ifEmpty: aBlock ifNotEmpty: anotherBlock\x0a\x09^ self isEmpty\x0a\x09\x09ifTrue: [ aBlock value ]\x0a\x09\x09ifFalse: [ anotherBlock value ]",
+messageSends: ["ifTrue:ifFalse:", "isEmpty", "value"],
+referencedClasses: []
+}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "ifNotEmpty:",
@@ -542,6 +566,30 @@ referencedClasses: []
 }),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "ifNotEmpty:ifEmpty:",
+category: 'testing',
+fn: function (aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1;
+$2=self._notEmpty();
+if(smalltalk.assert($2)){
+$1=_st(aBlock)._value();
+$ctx1.sendIdx["value"]=1;
+} else {
+$1=_st(anotherBlock)._value();
+};
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"ifNotEmpty:ifEmpty:",{aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.Collection)})},
+args: ["aBlock", "anotherBlock"],
+source: "ifNotEmpty: aBlock ifEmpty: anotherBlock\x0a\x09^ self notEmpty\x0a\x09\x09ifTrue: [ aBlock value ]\x0a\x09\x09ifFalse: [ anotherBlock value ]",
+messageSends: ["ifTrue:ifFalse:", "notEmpty", "value"],
+referencedClasses: []
+}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "includes:",

+ 56 - 6
js/Kernel-Tests.js

@@ -1503,7 +1503,7 @@ category: 'tests',
 fn: function (){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-var $3,$2,$1,$5,$4,$6,$9,$8,$7;
+var $3,$2,$1,$5,$4,$6,$9,$8,$7,$11,$10,$13,$12,$16,$15,$14,$18,$17,$19;
 $3=self._collectionClass();
 $ctx1.sendIdx["collectionClass"]=1;
 $2=_st($3)._new();
@@ -1534,16 +1534,66 @@ return smalltalk.withContext(function($ctx2) {
 return (42);
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,3)})}));
 $ctx1.sendIdx["ifNotEmpty:"]=1;
-self._assert_equals_($7,_st(self._collectionClass())._new());
+$11=self._collectionClass();
+$ctx1.sendIdx["collectionClass"]=3;
+$10=_st($11)._new();
+$ctx1.sendIdx["new"]=3;
+self._assert_equals_($7,$10);
 $ctx1.sendIdx["assert:equals:"]=3;
-self._assert_equals_(_st(self._collection())._ifNotEmpty_((function(){
+$13=self._collection();
+$ctx1.sendIdx["collection"]=3;
+$12=_st($13)._ifNotEmpty_((function(){
+return smalltalk.withContext(function($ctx2) {
+return (42);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,4)})}));
+self._assert_equals_($12,(42));
+$ctx1.sendIdx["assert:equals:"]=4;
+$16=self._collectionClass();
+$ctx1.sendIdx["collectionClass"]=4;
+$15=_st($16)._new();
+$ctx1.sendIdx["new"]=4;
+$14=_st($15)._ifEmpty_ifNotEmpty_((function(){
+return smalltalk.withContext(function($ctx2) {
+return (42);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,5)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
+return (999);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,6)})}));
+$ctx1.sendIdx["ifEmpty:ifNotEmpty:"]=1;
+self._assert_equals_($14,(42));
+$ctx1.sendIdx["assert:equals:"]=5;
+$18=self._collection();
+$ctx1.sendIdx["collection"]=4;
+$17=_st($18)._ifEmpty_ifNotEmpty_((function(){
 return smalltalk.withContext(function($ctx2) {
 return (42);
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1,4)})})),(42));
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,7)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
+return (999);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,8)})}));
+self._assert_equals_($17,(999));
+$ctx1.sendIdx["assert:equals:"]=6;
+$19=_st(_st(self._collectionClass())._new())._ifNotEmpty_ifEmpty_((function(){
+return smalltalk.withContext(function($ctx2) {
+return (42);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,9)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
+return (999);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,10)})}));
+$ctx1.sendIdx["ifNotEmpty:ifEmpty:"]=1;
+self._assert_equals_($19,(999));
+$ctx1.sendIdx["assert:equals:"]=7;
+self._assert_equals_(_st(self._collection())._ifNotEmpty_ifEmpty_((function(){
+return smalltalk.withContext(function($ctx2) {
+return (42);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,11)})}),(function(){
+return smalltalk.withContext(function($ctx2) {
+return (999);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,12)})})),(42));
 return self}, function($ctx1) {$ctx1.fill(self,"testIfEmptyFamily",{},smalltalk.CollectionTest)})},
 args: [],
-source: "testIfEmptyFamily\x0a\x09self assert: (self collectionClass new ifEmpty: [ 42 ]) equals: 42.\x0a\x09self assert: (self collection ifEmpty: [ 42 ]) equals: self collection.\x0a\x0a\x09self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.\x0a\x09self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.",
-messageSends: ["assert:equals:", "ifEmpty:", "new", "collectionClass", "collection", "ifNotEmpty:"],
+source: "testIfEmptyFamily\x0a\x09self assert: (self collectionClass new ifEmpty: [ 42 ]) equals: 42.\x0a\x09self assert: (self collection ifEmpty: [ 42 ]) equals: self collection.\x0a\x0a\x09self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.\x0a\x09self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.\x0a\x09\x0a\x09self assert: (self collectionClass new ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 42.\x0a\x09self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 999.\x0a\x0a\x09self assert: (self collectionClass new ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 999.\x0a\x09self assert: (self collection ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 42",
+messageSends: ["assert:equals:", "ifEmpty:", "new", "collectionClass", "collection", "ifNotEmpty:", "ifEmpty:ifNotEmpty:", "ifNotEmpty:ifEmpty:"],
 referencedClasses: []
 }),
 smalltalk.CollectionTest);

+ 12 - 0
st/Kernel-Collections.st

@@ -245,12 +245,24 @@ ifEmpty: aBlock
 		ifFalse: [ self ]
 !
 
+ifEmpty: aBlock ifNotEmpty: anotherBlock
+	^ self isEmpty
+		ifTrue: [ aBlock value ]
+		ifFalse: [ anotherBlock value ]
+!
+
 ifNotEmpty: aBlock
 	^ self notEmpty
 		ifTrue: [ aBlock value ]
 		ifFalse: [ self ]
 !
 
+ifNotEmpty: aBlock ifEmpty: anotherBlock
+	^ self notEmpty
+		ifTrue: [ aBlock value ]
+		ifFalse: [ anotherBlock value ]
+!
+
 includes: anObject
 	| sentinel |
 	sentinel := Object new.

+ 6 - 0
st/Kernel-Tests.st

@@ -469,6 +469,12 @@ testIfEmptyFamily
 
 	self assert: (self collectionClass new ifNotEmpty: [ 42 ]) equals: self collectionClass new.
 	self assert: (self collection ifNotEmpty: [ 42 ]) equals: 42.
+	
+	self assert: (self collectionClass new ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 42.
+	self assert: (self collection ifEmpty: [ 42 ] ifNotEmpty: [ 999 ]) equals: 999.
+
+	self assert: (self collectionClass new ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 999.
+	self assert: (self collection ifNotEmpty: [ 42 ] ifEmpty: [ 999 ]) equals: 42
 !
 
 testIsEmpty