Explorar o código

IndexableCollection >> {with:do:,withIndexDo:}

Herbert Vojčík %!s(int64=11) %!d(string=hai) anos
pai
achega
5f4ed73743
Modificáronse 3 ficheiros con 110 adicións e 0 borrados
  1. 37 0
      js/Kernel-Collections.deploy.js
  2. 52 0
      js/Kernel-Collections.js
  3. 21 0
      st/Kernel-Collections.st

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

@@ -795,6 +795,30 @@ smalltalk.Collection.klass);
 
 
 smalltalk.addClass('IndexableCollection', smalltalk.Collection, [], 'Kernel-Collections');
+smalltalk.addMethod(
+"_with_do_",
+smalltalk.method({
+selector: "with:do:",
+fn: function (anotherCollection,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._withIndexDo_((function(each,index){
+return smalltalk.withContext(function($ctx2) {
return _st(aBlock)._value_value_(each,_st(anotherCollection)._at_(index));
+}, function($ctx2) {$ctx2.fillBlock({each:each,index:index},$ctx1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"with:do:",{anotherCollection:anotherCollection,aBlock:aBlock}, smalltalk.IndexableCollection)})},
+messageSends: ["withIndexDo:", "value:value:", "at:"]}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_withIndexDo_",
+smalltalk.method({
+selector: "withIndexDo:",
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"withIndexDo:",{aBlock:aBlock}, smalltalk.IndexableCollection)})},
+messageSends: ["subclassReponsibility"]}),
+smalltalk.IndexableCollection);
+
 
 
 smalltalk.addClass('HashedCollection', smalltalk.IndexableCollection, [], 'Kernel-Collections');
@@ -1351,6 +1375,19 @@ return self}, function($ctx1) {$ctx1.fill(self,"valuesDo:",{aBlock:aBlock}, smal
 messageSends: ["keysAndValuesDo:", "value:"]}),
 smalltalk.HashedCollection);
 
+smalltalk.addMethod(
+"_withIndexDo_",
+smalltalk.method({
+selector: "withIndexDo:",
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._keysAndValuesDo_((function(key,value){
+return smalltalk.withContext(function($ctx2) {
return _st(aBlock)._value_value_(value,key);
+}, function($ctx2) {$ctx2.fillBlock({key:key,value:value},$ctx1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"withIndexDo:",{aBlock:aBlock}, smalltalk.HashedCollection)})},
+messageSends: ["keysAndValuesDo:", "value:value:"]}),
+smalltalk.HashedCollection);
+
 
 smalltalk.addMethod(
 "_fromPairs_",

+ 52 - 0
js/Kernel-Collections.js

@@ -1036,6 +1036,40 @@ smalltalk.Collection.klass);
 
 smalltalk.addClass('IndexableCollection', smalltalk.Collection, [], 'Kernel-Collections');
 smalltalk.IndexableCollection.comment="An IndexableCollection is a key-value store, that is,\x0ait stores values under indexes.\x0a\x0aAs a rule of thumb, if a collection has at: and at:put:,\x0ait is an IndexableCollection."
+smalltalk.addMethod(
+"_with_do_",
+smalltalk.method({
+selector: "with:do:",
+category: 'enumeration',
+fn: function (anotherCollection,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._withIndexDo_((function(each,index){
+return smalltalk.withContext(function($ctx2) {
return _st(aBlock)._value_value_(each,_st(anotherCollection)._at_(index));
+}, function($ctx2) {$ctx2.fillBlock({each:each,index:index},$ctx1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"with:do:",{anotherCollection:anotherCollection,aBlock:aBlock}, smalltalk.IndexableCollection)})},
+args: ["anotherCollection", "aBlock"],
+source: "with: anotherCollection do: aBlock\x0a\x09\x22Calls aBlock with every value from self\x0a\x09and with indetically-indexed value from anotherCollection\x22\x0a\x0a\x09self withIndexDo: [ :each :index |\x0a\x09\x09aBlock value: each value: (anotherCollection at: index) ]",
+messageSends: ["withIndexDo:", "value:value:", "at:"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_withIndexDo_",
+smalltalk.method({
+selector: "withIndexDo:",
+category: 'enumeration',
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"withIndexDo:",{aBlock:aBlock}, smalltalk.IndexableCollection)})},
+args: ["aBlock"],
+source: "withIndexDo: aBlock\x0a\x09\x22Calls aBlock with every value from self\x0a\x09and with its index as the second argument\x22\x0a\x0a\x09self subclassReponsibility",
+messageSends: ["subclassReponsibility"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
 
 
 smalltalk.addClass('HashedCollection', smalltalk.IndexableCollection, [], 'Kernel-Collections');
@@ -1758,6 +1792,24 @@ referencedClasses: []
 }),
 smalltalk.HashedCollection);
 
+smalltalk.addMethod(
+"_withIndexDo_",
+smalltalk.method({
+selector: "withIndexDo:",
+category: 'enumerating',
+fn: function (aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._keysAndValuesDo_((function(key,value){
+return smalltalk.withContext(function($ctx2) {
return _st(aBlock)._value_value_(value,key);
+}, function($ctx2) {$ctx2.fillBlock({key:key,value:value},$ctx1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"withIndexDo:",{aBlock:aBlock}, smalltalk.HashedCollection)})},
+args: ["aBlock"],
+source: "withIndexDo: aBlock\x0a\x09self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]",
+messageSends: ["keysAndValuesDo:", "value:value:"],
+referencedClasses: []
+}),
+smalltalk.HashedCollection);
+
 
 smalltalk.addMethod(
 "_fromPairs_",

+ 21 - 0
st/Kernel-Collections.st

@@ -312,6 +312,23 @@ it stores values under indexes.
 As a rule of thumb, if a collection has at: and at:put:,
 it is an IndexableCollection.!
 
+!IndexableCollection methodsFor: 'enumeration'!
+
+with: anotherCollection do: aBlock
+	"Calls aBlock with every value from self
+	and with indetically-indexed value from anotherCollection"
+
+	self withIndexDo: [ :each :index |
+		aBlock value: each value: (anotherCollection at: index) ]
+!
+
+withIndexDo: aBlock
+	"Calls aBlock with every value from self
+	and with its index as the second argument"
+
+	self subclassReponsibility
+! !
+
 IndexableCollection subclass: #HashedCollection
 	instanceVariableNames: ''
 	package: 'Kernel-Collections'!
@@ -502,6 +519,10 @@ select: aBlock
 
 valuesDo: aBlock
 	self keysAndValuesDo: [ :key :value | aBlock value: value ]
+!
+
+withIndexDo: aBlock
+	self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]
 ! !
 
 !HashedCollection methodsFor: 'printing'!