Browse Source

Merge pull request #480 from herby/gh-479

Implementing subclass responsibilities for at:ifPresent:ifAbsent:
Nicolas Petton 11 years ago
parent
commit
3fb637c993
3 changed files with 77 additions and 6 deletions
  1. 27 2
      js/Kernel-Collections.deploy.js
  2. 38 3
      js/Kernel-Collections.js
  3. 12 1
      st/Kernel-Collections.st

+ 27 - 2
js/Kernel-Collections.deploy.js

@@ -709,9 +709,9 @@ selector: "at:ifPresent:ifAbsent:",
 fn: function (anIndex,aBlock,anotherBlock){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-_st(self)._subclassReponsibility();
+_st(self)._subclassResponsibility();
 return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.IndexableCollection)})},
-messageSends: ["subclassReponsibility"]}),
+messageSends: ["subclassResponsibility"]}),
 smalltalk.IndexableCollection);
 
 smalltalk.addMethod(
@@ -2005,6 +2005,17 @@ return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{anIndex:anIndex,a
 messageSends: []}),
 smalltalk.Array);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+fn: function (anIndex,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.Array)})},
+messageSends: []}),
+smalltalk.Array);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "at:put:",
@@ -2618,6 +2629,20 @@ return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{anIndex:anIndex,a
 messageSends: []}),
 smalltalk.String);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+fn: function (anIndex,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+
+		var result = String(self).charAt(anIndex - 1);
+		return result ? aBlock._value_(result) : anotherBlock._value();
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.String)})},
+messageSends: []}),
+smalltalk.String);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "charCodeAt:",

+ 38 - 3
js/Kernel-Collections.js

@@ -938,11 +938,11 @@ category: 'accessing',
 fn: function (anIndex,aBlock,anotherBlock){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-_st(self)._subclassReponsibility();
+_st(self)._subclassResponsibility();
 return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.IndexableCollection)})},
 args: ["anIndex", "aBlock", "anotherBlock"],
-source: "at: anIndex ifPresent: aBlock ifAbsent: anotherBlock\x0a\x09\x22Lookup the given index in the receiver.\x0a\x09If it is present, answer the value of evaluating aBlock with the value stored at anIndex.\x0a\x09Otherwise, answer the value of anotherBlock.\x22\x0a\x0a\x09self subclassReponsibility",
-messageSends: ["subclassReponsibility"],
+source: "at: anIndex ifPresent: aBlock ifAbsent: anotherBlock\x0a\x09\x22Lookup the given index in the receiver.\x0a\x09If it is present, answer the value of evaluating aBlock with the value stored at anIndex.\x0a\x09Otherwise, answer the value of anotherBlock.\x22\x0a\x0a\x09self subclassResponsibility",
+messageSends: ["subclassResponsibility"],
 referencedClasses: []
 }),
 smalltalk.IndexableCollection);
@@ -2672,6 +2672,22 @@ referencedClasses: []
 }),
 smalltalk.Array);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+category: 'accessing',
+fn: function (anIndex,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.Array)})},
+args: ["anIndex", "aBlock", "anotherBlock"],
+source: "at: anIndex ifPresent: aBlock ifAbsent: anotherBlock\x0a\x09<return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Array);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "at:put:",
@@ -3522,6 +3538,25 @@ referencedClasses: []
 }),
 smalltalk.String);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+category: 'accessing',
+fn: function (anIndex,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+
+		var result = String(self).charAt(anIndex - 1);
+		return result ? aBlock._value_(result) : anotherBlock._value();
+	;
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock},smalltalk.String)})},
+args: ["anIndex", "aBlock", "anotherBlock"],
+source: "at: anIndex ifPresent: aBlock ifAbsent: anotherBlock\x0a\x09<\x0a\x09\x09var result = String(self).charAt(anIndex - 1);\x0a\x09\x09return result ? aBlock._value_(result) : anotherBlock._value();\x0a\x09>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.String);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "charCodeAt:",

+ 12 - 1
st/Kernel-Collections.st

@@ -315,7 +315,7 @@ at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
 	If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
 	Otherwise, answer the value of anotherBlock."
 
-	self subclassReponsibility
+	self subclassResponsibility
 !
 
 at: anIndex put: anObject
@@ -897,6 +897,10 @@ at: anIndex ifAbsent: aBlock
 	>
 !
 
+at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
+	<return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);>
+!
+
 at: anIndex put: anObject
 	<return self[anIndex - 1] = anObject>
 !
@@ -1105,6 +1109,13 @@ at: anIndex ifAbsent: aBlock
 	<return String(self).charAt(anIndex - 1) || aBlock()>
 !
 
+at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
+	<
+		var result = String(self).charAt(anIndex - 1);
+		return result ? aBlock._value_(result) : anotherBlock._value();
+	>
+!
+
 charCodeAt: anInteger
 	< return self.charCodeAt(anInteger - 1) >
 !