Browse Source

fixed issue #482

Nicolas Petton 11 years ago
parent
commit
335e222f8d
6 changed files with 147 additions and 124 deletions
  1. 13 26
      js/Kernel-Collections.deploy.js
  2. 15 33
      js/Kernel-Collections.js
  3. 42 20
      js/Kernel-Tests.deploy.js
  4. 52 25
      js/Kernel-Tests.js
  5. 9 13
      st/Kernel-Collections.st
  6. 16 7
      st/Kernel-Tests.st

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

@@ -144,29 +144,6 @@ 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",
@@ -250,10 +227,20 @@ fn: function (aBlock){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 var $1;
-$1=_st(self)._anySatisfy_(aBlock);
-return $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,"contains:",{aBlock:aBlock},smalltalk.Collection)})},
-messageSends: ["anySatisfy:"]}),
+messageSends: ["do:", "ifTrue:", "value:"]}),
 smalltalk.Collection);
 
 smalltalk.addMethod(

+ 15 - 33
js/Kernel-Collections.js

@@ -196,34 +196,6 @@ 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",
@@ -328,17 +300,27 @@ smalltalk.Collection);
 smalltalk.addMethod(
 smalltalk.method({
 selector: "contains:",
-category: 'enumerating',
+category: 'testing',
 fn: function (aBlock){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 var $1;
-$1=_st(self)._anySatisfy_(aBlock);
-return $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,"contains:",{aBlock:aBlock},smalltalk.Collection)})},
 args: ["aBlock"],
-source: "contains: aBlock\x0a\x09^ self anySatisfy: aBlock",
-messageSends: ["anySatisfy:"],
+source: "contains: 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);

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

@@ -779,26 +779,6 @@ 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",
@@ -992,6 +972,27 @@ return self}, function($ctx1) {$ctx1.fill(self,"testAtIfAbsent",{},smalltalk.Ind
 messageSends: ["assert:equals:", "at:ifAbsent:", "+", "size", "collection"]}),
 smalltalk.IndexableCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testContains",
+fn: function (){
+var self=this;
+var collection;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+collection=_st(self)._collection();
+_st(self)._assert_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st(_st(self)._collection())._first());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+_st(self)._deny_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st($Object())._new());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testContains",{collection:collection},smalltalk.IndexableCollectionTest)})},
+messageSends: ["collection", "assert:", "contains:", "=", "first", "deny:", "new"]}),
+smalltalk.IndexableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testIndexOf",
@@ -1073,6 +1074,27 @@ return self}, function($ctx1) {$ctx1.fill(self,"testAt",{},smalltalk.HashedColle
 messageSends: ["assert:equals:", "at:", "collection", "should:raise:"]}),
 smalltalk.HashedCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testContains",
+fn: function (){
+var self=this;
+var collection;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+collection=_st(self)._collection();
+_st(self)._assert_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st(_st(_st(self)._collection())._values())._first());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+_st(self)._deny_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st($Object())._new());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testContains",{collection:collection},smalltalk.HashedCollectionTest)})},
+messageSends: ["collection", "assert:", "contains:", "=", "first", "values", "deny:", "new"]}),
+smalltalk.HashedCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testIndexOf",

+ 52 - 25
js/Kernel-Tests.js

@@ -944,31 +944,6 @@ 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",
@@ -1227,6 +1202,32 @@ referencedClasses: []
 }),
 smalltalk.IndexableCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testContains",
+category: 'tests',
+fn: function (){
+var self=this;
+var collection;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+collection=_st(self)._collection();
+_st(self)._assert_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st(_st(self)._collection())._first());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+_st(self)._deny_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st($Object())._new());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testContains",{collection:collection},smalltalk.IndexableCollectionTest)})},
+args: [],
+source: "testContains\x0a\x09| collection |\x0a\x09collection := self collection.\x0a\x09\x0a\x09self assert: (self collection contains: [ :each | each = self collection first ]).\x0a\x09self deny: (self collection contains: [ :each | each = Object new ])",
+messageSends: ["collection", "assert:", "contains:", "=", "first", "deny:", "new"],
+referencedClasses: ["Object"]
+}),
+smalltalk.IndexableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testIndexOf",
@@ -1333,6 +1334,32 @@ referencedClasses: ["Error"]
 }),
 smalltalk.HashedCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testContains",
+category: 'tests',
+fn: function (){
+var self=this;
+var collection;
+function $Object(){return smalltalk.Object||(typeof Object=="undefined"?nil:Object)}
+return smalltalk.withContext(function($ctx1) { 
+collection=_st(self)._collection();
+_st(self)._assert_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st(_st(_st(self)._collection())._values())._first());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+_st(self)._deny_(_st(_st(self)._collection())._contains_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(each).__eq(_st($Object())._new());
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})})));
+return self}, function($ctx1) {$ctx1.fill(self,"testContains",{collection:collection},smalltalk.HashedCollectionTest)})},
+args: [],
+source: "testContains\x0a\x09| collection |\x0a\x09collection := self collection.\x0a\x09\x0a\x09self assert: (self collection contains: [ :each | each = self collection values first ]).\x0a\x09self deny: (self collection contains: [ :each | each = Object new ])",
+messageSends: ["collection", "assert:", "contains:", "=", "first", "values", "deny:", "new"],
+referencedClasses: ["Object"]
+}),
+smalltalk.HashedCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testIndexOf",

+ 9 - 13
st/Kernel-Collections.st

@@ -136,15 +136,6 @@ 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.
@@ -153,10 +144,6 @@ collect: aBlock
 	^stream contents
 !
 
-contains: aBlock
-	^ self anySatisfy: aBlock
-!
-
 detect: aBlock
 	^self detect: aBlock ifNone: [self errorNotFound]
 !
@@ -228,6 +215,15 @@ putOn: aStream
 
 !Collection methodsFor: 'testing'!
 
+contains: 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
+!
+
 ifEmpty: aBlock
 	"Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver. Note that the fact that this method returns its argument in case the receiver is not empty allows one to write expressions like the following ones: self classifyMethodAs:
 		(myProtocol ifEmpty: ['As yet unclassified'])"

+ 16 - 7
st/Kernel-Tests.st

@@ -343,13 +343,6 @@ 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
@@ -443,6 +436,14 @@ testAtIfAbsent
 	self assert: (self collection at: (self collection size + 1) ifAbsent: [ 'none' ]) equals: 'none'
 !
 
+testContains
+	| collection |
+	collection := self collection.
+	
+	self assert: (self collection contains: [ :each | each = self collection first ]).
+	self deny: (self collection contains: [ :each | each = Object new ])
+!
+
 testIndexOf
 	self assert: (self collection indexOf: 2) equals: 2.
 	self should: [ self collection indexOf: 999 ] raise: Error.
@@ -478,6 +479,14 @@ testAt
 	self should: [ self collection at: 5 ] raise: Error
 !
 
+testContains
+	| collection |
+	collection := self collection.
+	
+	self assert: (self collection contains: [ :each | each = self collection values first ]).
+	self deny: (self collection contains: [ :each | each = Object new ])
+!
+
 testIndexOf
 	self assert: (self collection indexOf: 2) equals: 'a'.
 	self should: [ self collection indexOf: 999 ] raise: Error.