Browse Source

fixed issue 482

Clement Bera 11 years ago
parent
commit
21d0890cc9

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

@@ -144,6 +144,29 @@ return $1;
 messageSends: ["do:", "add:"]}),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "anySatisfy:",
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+var $early={};
+try {
+_st(self)._do_((function(each){
+return smalltalk.withContext(function($ctx2) {
+$1=_st(aBlock)._value_(each);
+if(smalltalk.assert($1)){
+throw $early=[true];
+};
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
+return false;
+}
+catch(e) {if(e===$early)return e[0]; throw e}
+}, function($ctx1) {$ctx1.fill(self,"anySatisfy:",{aBlock:aBlock},smalltalk.Collection)})},
+messageSends: ["do:", "ifTrue:", "value:"]}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "asArray",
@@ -220,6 +243,19 @@ return $1;
 messageSends: ["writeStream", "new", "class", "do:", "nextPut:", "value:", "contents"]}),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "contains:",
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=_st(self)._anySatisfy_(aBlock);
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"contains:",{aBlock:aBlock},smalltalk.Collection)})},
+messageSends: ["anySatisfy:"]}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "copyWith:",

+ 46 - 0
js/Kernel-Collections.js

@@ -196,6 +196,34 @@ referencedClasses: []
 }),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "anySatisfy:",
+category: 'enumerating',
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+var $early={};
+try {
+_st(self)._do_((function(each){
+return smalltalk.withContext(function($ctx2) {
+$1=_st(aBlock)._value_(each);
+if(smalltalk.assert($1)){
+throw $early=[true];
+};
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
+return false;
+}
+catch(e) {if(e===$early)return e[0]; throw e}
+}, function($ctx1) {$ctx1.fill(self,"anySatisfy:",{aBlock:aBlock},smalltalk.Collection)})},
+args: ["aBlock"],
+source: "anySatisfy: aBlock\x0a\x09\x22Evaluate aBlock with the elements of the receiver.\x0a\x09If aBlock returns true for any element return true.\x0a\x09Otherwise return false.\x22\x0a\x0a\x09self do: [ :each | (aBlock value: each) ifTrue: [ ^ true ] ].\x0a\x09^ false",
+messageSends: ["do:", "ifTrue:", "value:"],
+referencedClasses: []
+}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "asArray",
@@ -297,6 +325,24 @@ referencedClasses: []
 }),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "contains:",
+category: 'enumerating',
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=_st(self)._anySatisfy_(aBlock);
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"contains:",{aBlock:aBlock},smalltalk.Collection)})},
+args: ["aBlock"],
+source: "contains: aBlock\x0a\x09^ self anySatisfy: aBlock",
+messageSends: ["anySatisfy:"],
+referencedClasses: []
+}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "copyWith:",

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

@@ -779,6 +779,26 @@ return false;
 messageSends: []}),
 smalltalk.CollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAnySatisfy",
+fn: function (){
+var self=this;
+var newCollection;
+return smalltalk.withContext(function($ctx1) { 
+newCollection=[(1), (2), (3), (4)];
+_st(self)._assert_(_st(newCollection)._anySatisfy_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq((3));
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+_st(self)._deny_(_st(newCollection)._anySatisfy_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each)._isString();
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testAnySatisfy",{newCollection:newCollection},smalltalk.CollectionTest)})},
+messageSends: ["assert:", "anySatisfy:", "=", "deny:", "isString"]}),
+smalltalk.CollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testAsArray",

+ 25 - 0
js/Kernel-Tests.js

@@ -944,6 +944,31 @@ referencedClasses: []
 }),
 smalltalk.CollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAnySatisfy",
+category: 'tests',
+fn: function (){
+var self=this;
+var newCollection;
+return smalltalk.withContext(function($ctx1) { 
+newCollection=[(1), (2), (3), (4)];
+_st(self)._assert_(_st(newCollection)._anySatisfy_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq((3));
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+_st(self)._deny_(_st(newCollection)._anySatisfy_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each)._isString();
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testAnySatisfy",{newCollection:newCollection},smalltalk.CollectionTest)})},
+args: [],
+source: "testAnySatisfy\x0a\x09| newCollection |\x0a\x09newCollection := #(1 2 3 4).\x0a\x09self assert: (newCollection anySatisfy: [ :each | each = 3 ]).\x0a\x09self deny: (newCollection anySatisfy: [ :each | each isString ]).",
+messageSends: ["assert:", "anySatisfy:", "=", "deny:", "isString"],
+referencedClasses: []
+}),
+smalltalk.CollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testAsArray",

+ 13 - 0
st/Kernel-Collections.st

@@ -136,6 +136,15 @@ copyWithoutAll: aCollection
 
 !Collection methodsFor: 'enumerating'!
 
+anySatisfy: aBlock
+	"Evaluate aBlock with the elements of the receiver.
+	If aBlock returns true for any element return true.
+	Otherwise return false."
+
+	self do: [ :each | (aBlock value: each) ifTrue: [ ^ true ] ].
+	^ false
+!
+
 collect: aBlock
 	| stream |
 	stream := self class new writeStream.
@@ -144,6 +153,10 @@ collect: aBlock
 	^stream contents
 !
 
+contains: aBlock
+	^ self anySatisfy: aBlock
+!
+
 detect: aBlock
 	^self detect: aBlock ifNone: [self errorNotFound]
 !

+ 7 - 0
st/Kernel-Tests.st

@@ -343,6 +343,13 @@ isCollectionReadOnly
 
 !CollectionTest methodsFor: 'tests'!
 
+testAnySatisfy
+	| newCollection |
+	newCollection := #(1 2 3 4).
+	self assert: (newCollection anySatisfy: [ :each | each = 3 ]).
+	self deny: (newCollection anySatisfy: [ :each | each isString ]).
+!
+
 testAsArray
 	self
 		assertSameContents: self collection