Browse Source

Collection >> anyOne

Herbert Vojčík 10 years ago
parent
commit
fc0c32187a
4 changed files with 67 additions and 0 deletions
  1. 27 0
      js/Kernel-Collections.js
  2. 26 0
      js/Kernel-Tests.js
  3. 9 0
      st/Kernel-Collections.st
  4. 5 0
      st/Kernel-Tests.st

+ 27 - 0
js/Kernel-Collections.js

@@ -209,6 +209,33 @@ referencedClasses: []
 }),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "anyOne",
+category: 'adding/removing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $early={};
+try {
+self._ifEmpty_((function(){
+return smalltalk.withContext(function($ctx2) {
+return self._error_("Collection is empty");
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}));
+self._do_((function(each){
+return smalltalk.withContext(function($ctx2) {
+throw $early=[each];
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,2)})}));
+return self}
+catch(e) {if(e===$early)return e[0]; throw e}
+}, function($ctx1) {$ctx1.fill(self,"anyOne",{},smalltalk.Collection)})},
+args: [],
+source: "anyOne\x0a\x09\x22Answer a representative sample of the receiver. This method can\x0a\x09be helpful when needing to preinfer the nature of the contents of \x0a\x09semi-homogeneous collections.\x22\x0a\x0a\x09self ifEmpty: [ self error: 'Collection is empty' ].\x0a\x09self do: [:each | ^ each]",
+messageSends: ["ifEmpty:", "error:", "do:"],
+referencedClasses: []
+}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "asArray",

+ 26 - 0
js/Kernel-Tests.js

@@ -1356,6 +1356,32 @@ referencedClasses: []
 }),
 smalltalk.CollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testAnyOne",
+category: 'tests',
+fn: function (){
+var self=this;
+function $Error(){return smalltalk.Error||(typeof Error=="undefined"?nil:Error)}
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1;
+self._should_raise_((function(){
+return smalltalk.withContext(function($ctx2) {
+return _st(_st(self._collectionClass())._new())._anyOne();
+$ctx2.sendIdx["anyOne"]=1;
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}),$Error());
+$2=self._collection();
+$ctx1.sendIdx["collection"]=1;
+$1=_st($2)._includes_(_st(self._collection())._anyOne());
+self._assert_($1);
+return self}, function($ctx1) {$ctx1.fill(self,"testAnyOne",{},smalltalk.CollectionTest)})},
+args: [],
+source: "testAnyOne\x0a\x09self should: [ self collectionClass new anyOne ] raise: Error.\x0a\x09self assert: (self collection includes: self collection anyOne)",
+messageSends: ["should:raise:", "anyOne", "new", "collectionClass", "assert:", "includes:", "collection"],
+referencedClasses: ["Error"]
+}),
+smalltalk.CollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testAsArray",

+ 9 - 0
st/Kernel-Collections.st

@@ -85,6 +85,15 @@ addAll: aCollection
 	^ aCollection
 !
 
+anyOne
+	"Answer a representative sample of the receiver. This method can
+	be helpful when needing to preinfer the nature of the contents of 
+	semi-homogeneous collections."
+
+	self ifEmpty: [ self error: 'Collection is empty' ].
+	self do: [:each | ^ each]
+!
+
 remove: anObject
 	^ self remove: anObject ifAbsent: [ self errorNotFound ]
 !

+ 5 - 0
st/Kernel-Tests.st

@@ -416,6 +416,11 @@ isCollectionReadOnly
 
 !CollectionTest methodsFor: 'tests'!
 
+testAnyOne
+	self should: [ self collectionClass new anyOne ] raise: Error.
+	self assert: (self collection includes: self collection anyOne)
+!
+
 testAsArray
 	self
 		assertSameContents: self collection