Browse Source

ifNotEmpty: returns self if empty

Herbert Vojčík 10 years ago
parent
commit
3c76e1637e
2 changed files with 14 additions and 7 deletions
  1. 11 6
      js/Kernel-Collections.js
  2. 3 1
      st/Kernel-Collections.st

+ 11 - 6
js/Kernel-Collections.js

@@ -526,13 +526,18 @@ category: 'testing',
 fn: function (aBlock){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-var $1;
-$1=self._notEmpty();
-_st($1)._ifTrue_(aBlock);
-return self}, function($ctx1) {$ctx1.fill(self,"ifNotEmpty:",{aBlock:aBlock},smalltalk.Collection)})},
+var $2,$1;
+$2=self._notEmpty();
+if(smalltalk.assert($2)){
+$1=_st(aBlock)._value();
+} else {
+$1=self;
+};
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"ifNotEmpty:",{aBlock:aBlock},smalltalk.Collection)})},
 args: ["aBlock"],
-source: "ifNotEmpty: aBlock\x0a\x09self notEmpty ifTrue: aBlock.",
-messageSends: ["ifTrue:", "notEmpty"],
+source: "ifNotEmpty: aBlock\x0a\x09^ self notEmpty\x0a\x09\x09ifTrue: [ aBlock value ]\x0a\x09\x09ifFalse: [ self ]",
+messageSends: ["ifTrue:ifFalse:", "notEmpty", "value"],
 referencedClasses: []
 }),
 smalltalk.Collection);

+ 3 - 1
st/Kernel-Collections.st

@@ -246,7 +246,9 @@ ifEmpty: aBlock
 !
 
 ifNotEmpty: aBlock
-	self notEmpty ifTrue: aBlock.
+	^ self notEmpty
+		ifTrue: [ aBlock value ]
+		ifFalse: [ self ]
 !
 
 includes: anObject