Selaa lähdekoodia

Add some convenience messages to HashedCollection

HashedCollection already has a message for instance creation from a
collection of associations: #fromPairs. As @herby mentioned in #433,
this message is poorly named, as the associations are already "paired"
up.

I've renamed this message to #newFrom, and it accepts a collection of
associations. I've also added a message for creating a HashedCollection
or one of its derivatives from a flattened set of associations called
\#newFromPairs. The message accepts an array where every 2 elements form
an association - the odd element being the key and the even the value.
Of course this means that #newFromPairs only accepts arrays that have an
even size.
Kenneth Pullen 11 vuotta sitten
vanhempi
commit
11d7c62145

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

@@ -1319,6 +1319,7 @@ var self=this;
 var dict;
 return smalltalk.withContext(function($ctx1) { 
 var $1;
+_st(self)._deprecatedAPI();
 dict=_st(self)._new();
 _st(aCollection)._do_((function(each){
 return smalltalk.withContext(function($ctx2) {
@@ -1327,9 +1328,52 @@ return _st(dict)._add_(each);
 $1=dict;
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"fromPairs:",{aCollection:aCollection,dict:dict},smalltalk.HashedCollection.klass)})},
+messageSends: ["deprecatedAPI", "new", "do:", "add:"]}),
+smalltalk.HashedCollection.klass);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "newFrom:",
+fn: function (aCollection){
+var self=this;
+var newCollection;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+newCollection=_st(self)._new();
+_st(aCollection)._do_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(newCollection)._add_(each);
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
+$1=newCollection;
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"newFrom:",{aCollection:aCollection,newCollection:newCollection},smalltalk.HashedCollection.klass)})},
 messageSends: ["new", "do:", "add:"]}),
 smalltalk.HashedCollection.klass);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "newFromPairs:",
+fn: function (aCollection){
+var self=this;
+var newCollection;
+function $Error(){return smalltalk.Error||(typeof Error=="undefined"?nil:Error)}
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2;
+$1=_st(_st(aCollection)._size())._even();
+if(! smalltalk.assert($1)){
+_st($Error())._signal_("#newFromPairs only accepts arrays of an even length");
+};
+newCollection=_st(self)._new();
+_st(_st((1))._to_by_(_st(aCollection)._size(),(2)))._do_((function(keyIndex){
+return smalltalk.withContext(function($ctx2) {
+return _st(newCollection)._at_put_(_st(aCollection)._at_(keyIndex),_st(aCollection)._at_(_st(keyIndex).__plus((1))));
+}, function($ctx2) {$ctx2.fillBlock({keyIndex:keyIndex},$ctx1)})}));
+$2=newCollection;
+return $2;
+}, function($ctx1) {$ctx1.fill(self,"newFromPairs:",{aCollection:aCollection,newCollection:newCollection},smalltalk.HashedCollection.klass)})},
+messageSends: ["ifFalse:", "signal:", "even", "size", "new", "do:", "at:put:", "at:", "+", "to:by:"]}),
+smalltalk.HashedCollection.klass);
+
 
 smalltalk.addClass('Dictionary', smalltalk.HashedCollection, ['keys', 'values'], 'Kernel-Collections');
 smalltalk.addMethod(

+ 55 - 1
js/Kernel-Collections.js

@@ -1749,6 +1749,7 @@ var self=this;
 var dict;
 return smalltalk.withContext(function($ctx1) { 
 var $1;
+_st(self)._deprecatedAPI();
 dict=_st(self)._new();
 _st(aCollection)._do_((function(each){
 return smalltalk.withContext(function($ctx2) {
@@ -1758,12 +1759,65 @@ $1=dict;
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"fromPairs:",{aCollection:aCollection,dict:dict},smalltalk.HashedCollection.klass)})},
 args: ["aCollection"],
-source: "fromPairs: aCollection\x0a\x09| dict |\x0a\x09dict := self new.\x0a\x09aCollection do: [:each | dict add: each].\x0a\x09^dict",
+source: "fromPairs: aCollection\x0a\x09\x22This message is poorly named and has been replaced by #newFrom:\x22\x0a\x09| dict |\x0a\x09self deprecatedAPI.\x0a\x09dict := self new.\x0a\x09aCollection do: [:each | dict add: each].\x0a\x09^dict",
+messageSends: ["deprecatedAPI", "new", "do:", "add:"],
+referencedClasses: []
+}),
+smalltalk.HashedCollection.klass);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "newFrom:",
+category: 'instance creation',
+fn: function (aCollection){
+var self=this;
+var newCollection;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+newCollection=_st(self)._new();
+_st(aCollection)._do_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return _st(newCollection)._add_(each);
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
+$1=newCollection;
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"newFrom:",{aCollection:aCollection,newCollection:newCollection},smalltalk.HashedCollection.klass)})},
+args: ["aCollection"],
+source: "newFrom: aCollection\x0a| newCollection |\x0anewCollection := self new.\x0aaCollection do: [:each | newCollection add: each].\x0a^ newCollection.",
 messageSends: ["new", "do:", "add:"],
 referencedClasses: []
 }),
 smalltalk.HashedCollection.klass);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "newFromPairs:",
+category: 'instance creation',
+fn: function (aCollection){
+var self=this;
+var newCollection;
+function $Error(){return smalltalk.Error||(typeof Error=="undefined"?nil:Error)}
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2;
+$1=_st(_st(aCollection)._size())._even();
+if(! smalltalk.assert($1)){
+_st($Error())._signal_("#newFromPairs only accepts arrays of an even length");
+};
+newCollection=_st(self)._new();
+_st(_st((1))._to_by_(_st(aCollection)._size(),(2)))._do_((function(keyIndex){
+return smalltalk.withContext(function($ctx2) {
+return _st(newCollection)._at_put_(_st(aCollection)._at_(keyIndex),_st(aCollection)._at_(_st(keyIndex).__plus((1))));
+}, function($ctx2) {$ctx2.fillBlock({keyIndex:keyIndex},$ctx1)})}));
+$2=newCollection;
+return $2;
+}, function($ctx1) {$ctx1.fill(self,"newFromPairs:",{aCollection:aCollection,newCollection:newCollection},smalltalk.HashedCollection.klass)})},
+args: ["aCollection"],
+source: "newFromPairs: aCollection\x0a\x22Accept an array of elements where every two elements form an association - the odd element being the key, and the even element the value.\x22\x0a| newCollection |\x0aaCollection size even ifFalse: [ Error signal: '#newFromPairs only accepts arrays of an even length' ].\x0anewCollection := self new.\x0a( 1 to: aCollection size by: 2 ) do: [ :keyIndex | newCollection at: ( aCollection at: keyIndex ) put: ( aCollection at: keyIndex + 1 ) ].\x0a^ newCollection.",
+messageSends: ["ifFalse:", "signal:", "even", "size", "new", "do:", "at:put:", "at:", "+", "to:by:"],
+referencedClasses: ["Error"]
+}),
+smalltalk.HashedCollection.klass);
+
 
 smalltalk.addClass('Dictionary', smalltalk.HashedCollection, ['keys', 'values'], 'Kernel-Collections');
 smalltalk.Dictionary.comment="I represent a set of elements that can be viewed from one of two perspectives: a set of associations,\x0aor a container of values that are externally named where the name can be any object that responds to `=`.\x0a\x0aThe external name is referred to as the key.";

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

@@ -1137,6 +1137,32 @@ return self}, function($ctx1) {$ctx1.fill(self,"testIndexOf",{},smalltalk.Hashed
 messageSends: ["assert:equals:", "indexOf:", "collection", "should:raise:", "indexOf:ifAbsent:"]}),
 smalltalk.HashedCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testNewFrom",
+fn: function (){
+var self=this;
+var associations;
+return smalltalk.withContext(function($ctx1) { 
+associations=[_st("a").__minus_gt((1)),_st("b").__minus_gt((2))];
+_st(self)._assertSameContents_as_(_st(_st(_st(self)._class())._collectionClass())._newFrom_(associations),smalltalk.HashedCollection._fromPairs_([_st("a").__minus_gt((1)),_st("b").__minus_gt((2))]));
+return self}, function($ctx1) {$ctx1.fill(self,"testNewFrom",{associations:associations},smalltalk.HashedCollectionTest)})},
+messageSends: ["->", "assertSameContents:as:", "newFrom:", "collectionClass", "class"]}),
+smalltalk.HashedCollectionTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testNewFromPairs",
+fn: function (){
+var self=this;
+var flattenedAssociations;
+return smalltalk.withContext(function($ctx1) { 
+flattenedAssociations=["a",(1),"b",(2)];
+_st(self)._assertSameContents_as_(_st(_st(_st(self)._class())._collectionClass())._newFromPairs_(flattenedAssociations),smalltalk.HashedCollection._fromPairs_([_st("a").__minus_gt((1)),_st("b").__minus_gt((2))]));
+return self}, function($ctx1) {$ctx1.fill(self,"testNewFromPairs",{flattenedAssociations:flattenedAssociations},smalltalk.HashedCollectionTest)})},
+messageSends: ["assertSameContents:as:", "newFromPairs:", "collectionClass", "class", "->"]}),
+smalltalk.HashedCollectionTest);
+
 
 smalltalk.addMethod(
 smalltalk.method({

+ 36 - 0
js/Kernel-Tests.js

@@ -1412,6 +1412,42 @@ referencedClasses: ["Error"]
 }),
 smalltalk.HashedCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testNewFrom",
+category: 'tests',
+fn: function (){
+var self=this;
+var associations;
+return smalltalk.withContext(function($ctx1) { 
+associations=[_st("a").__minus_gt((1)),_st("b").__minus_gt((2))];
+_st(self)._assertSameContents_as_(_st(_st(_st(self)._class())._collectionClass())._newFrom_(associations),smalltalk.HashedCollection._fromPairs_([_st("a").__minus_gt((1)),_st("b").__minus_gt((2))]));
+return self}, function($ctx1) {$ctx1.fill(self,"testNewFrom",{associations:associations},smalltalk.HashedCollectionTest)})},
+args: [],
+source: "testNewFrom\x0a\x22Accept a collection of associations.\x22\x0a| associations |\x0aassociations := { 'a' -> 1. 'b' -> 2 }.\x0aself assertSameContents: ( self class collectionClass newFrom: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.",
+messageSends: ["->", "assertSameContents:as:", "newFrom:", "collectionClass", "class"],
+referencedClasses: []
+}),
+smalltalk.HashedCollectionTest);
+
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testNewFromPairs",
+category: 'tests',
+fn: function (){
+var self=this;
+var flattenedAssociations;
+return smalltalk.withContext(function($ctx1) { 
+flattenedAssociations=["a",(1),"b",(2)];
+_st(self)._assertSameContents_as_(_st(_st(_st(self)._class())._collectionClass())._newFromPairs_(flattenedAssociations),smalltalk.HashedCollection._fromPairs_([_st("a").__minus_gt((1)),_st("b").__minus_gt((2))]));
+return self}, function($ctx1) {$ctx1.fill(self,"testNewFromPairs",{flattenedAssociations:flattenedAssociations},smalltalk.HashedCollectionTest)})},
+args: [],
+source: "testNewFromPairs\x0a\x22Accept an array in which all odd indexes are keys and evens are values.\x22\x0a| flattenedAssociations |\x0aflattenedAssociations := { 'a'. 1. 'b'. 2 }.\x0aself assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.",
+messageSends: ["assertSameContents:as:", "newFromPairs:", "collectionClass", "class", "->"],
+referencedClasses: []
+}),
+smalltalk.HashedCollectionTest);
+
 
 smalltalk.addMethod(
 smalltalk.method({

+ 18 - 0
st/Kernel-Collections.st

@@ -579,10 +579,28 @@ includesKey: aKey
 !HashedCollection class methodsFor: 'instance creation'!
 
 fromPairs: aCollection
+	"This message is poorly named and has been replaced by #newFrom:"
 	| dict |
+	self deprecatedAPI.
 	dict := self new.
 	aCollection do: [:each | dict add: each].
 	^dict
+!
+
+newFrom: aCollection
+| newCollection |
+newCollection := self new.
+aCollection do: [:each | newCollection add: each].
+^ newCollection.
+!
+
+newFromPairs: aCollection
+"Accept an array of elements where every two elements form an association - the odd element being the key, and the even element the value."
+| newCollection |
+aCollection size even ifFalse: [ Error signal: '#newFromPairs only accepts arrays of an even length' ].
+newCollection := self new.
+( 1 to: aCollection size by: 2 ) do: [ :keyIndex | newCollection at: ( aCollection at: keyIndex ) put: ( aCollection at: keyIndex + 1 ) ].
+^ newCollection.
 ! !
 
 HashedCollection subclass: #Dictionary

+ 14 - 0
st/Kernel-Tests.st

@@ -507,6 +507,20 @@ testIndexOf
 	self assert: (self collection indexOf: 2) equals: 'a'.
 	self should: [ self collection indexOf: 999 ] raise: Error.
 	self assert: (self collection indexOf: 999 ifAbsent: [ 'sentinel' ]) equals: 'sentinel'
+!
+
+testNewFrom
+"Accept a collection of associations."
+| associations |
+associations := { 'a' -> 1. 'b' -> 2 }.
+self assertSameContents: ( self class collectionClass newFrom: associations ) as: #{ 'a' -> 1. 'b' -> 2 }.
+!
+
+testNewFromPairs
+"Accept an array in which all odd indexes are keys and evens are values."
+| flattenedAssociations |
+flattenedAssociations := { 'a'. 1. 'b'. 2 }.
+self assertSameContents: ( self class collectionClass newFromPairs: flattenedAssociations ) as: #{ 'a' -> 1. 'b' -> 2 }.
 ! !
 
 !HashedCollectionTest class methodsFor: 'accessing'!