1
0
Nicolas Petton 11 سال پیش
والد
کامیت
cdaeb205e7
4فایلهای تغییر یافته به همراه136 افزوده شده و 12 حذف شده
  1. 39 8
      js/Kernel-Collections.js
  2. 66 0
      js/Kernel-Tests.js
  3. 15 4
      st/Kernel-Collections.st
  4. 16 0
      st/Kernel-Tests.st

+ 39 - 8
js/Kernel-Collections.js

@@ -2608,16 +2608,20 @@ smalltalk.addMethod(
 smalltalk.method({
 selector: "first:",
 protocol: 'accessing',
-fn: function (n){
+fn: function (aNumber){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
-var $1;
-$1=self._copyFrom_to_((1),n);
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"first:",{n:n},smalltalk.SequenceableCollection)})},
-args: ["n"],
-source: "first: n\x0a\x09\x22Answer the first n elements of the receiver.\x0a\x09Raise an error if there are not enough elements.\x22\x0a\x0a\x09^ self copyFrom: 1 to: n",
-messageSends: ["copyFrom:to:"],
+var $1,$2;
+$1=_st(self._size()).__lt(aNumber);
+if(smalltalk.assert($1)){
+self._error_("Invalid number of elements");
+};
+$2=self._copyFrom_to_((1),aNumber);
+return $2;
+}, function($ctx1) {$ctx1.fill(self,"first:",{aNumber:aNumber},smalltalk.SequenceableCollection)})},
+args: ["aNumber"],
+source: "first: aNumber\x0a\x09\x22Answer the first `aNumber` elements of the receiver.\x0a\x09Raise an error if there are not enough elements in the receiver.\x22\x0a\x0a\x09self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].\x0a\x0a\x09^ self copyFrom: 1 to: aNumber",
+messageSends: ["ifTrue:", "<", "size", "error:", "copyFrom:to:"],
 referencedClasses: []
 }),
 smalltalk.SequenceableCollection);
@@ -2744,6 +2748,33 @@ referencedClasses: []
 }),
 smalltalk.SequenceableCollection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "last:",
+protocol: 'accessing',
+fn: function (aNumber){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1,$5,$4,$3;
+$2=self._size();
+$ctx1.sendIdx["size"]=1;
+$1=_st($2).__lt(aNumber);
+if(smalltalk.assert($1)){
+self._error_("Invalid number of elements");
+};
+$5=self._size();
+$ctx1.sendIdx["size"]=2;
+$4=_st($5).__minus(aNumber);
+$3=self._copyFrom_to_($4,self._size());
+return $3;
+}, function($ctx1) {$ctx1.fill(self,"last:",{aNumber:aNumber},smalltalk.SequenceableCollection)})},
+args: ["aNumber"],
+source: "last: aNumber\x0a\x09\x22Answer the last aNumber elements of the receiver.\x0a\x09Raise an error if there are not enough elements in the receiver.\x22\x0a\x0a\x09self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].\x0a\x0a\x09^ self copyFrom: self size - aNumber to: self size",
+messageSends: ["ifTrue:", "<", "size", "error:", "copyFrom:to:", "-"],
+referencedClasses: []
+}),
+smalltalk.SequenceableCollection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "newStream",

+ 66 - 0
js/Kernel-Tests.js

@@ -2757,6 +2757,35 @@ referencedClasses: []
 }),
 smalltalk.SequenceableCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testFirstN",
+protocol: 'tests',
+fn: function (){
+var self=this;
+function $Error(){return smalltalk.Error||(typeof Error=="undefined"?nil:Error)}
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1,$4,$3;
+$2=self._collection();
+$ctx1.sendIdx["collection"]=1;
+$1=_st($2)._first_((2));
+$ctx1.sendIdx["first:"]=1;
+$4=self._collection();
+$ctx1.sendIdx["collection"]=2;
+$3=_st($4)._copyFrom_to_((0),(2));
+self._assert_equals_($1,$3);
+self._should_raise_((function(){
+return smalltalk.withContext(function($ctx2) {
+return _st(self._collection())._first_((33));
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}),$Error());
+return self}, function($ctx1) {$ctx1.fill(self,"testFirstN",{},smalltalk.SequenceableCollectionTest)})},
+args: [],
+source: "testFirstN\x0a\x09self \x0a\x09\x09assert: (self collection first: 2) \x0a\x09\x09equals: (self collection copyFrom: 0 to: 2).\x0a\x09\x09\x0a\x09self should: [ self collection first: 33 ] raise: Error",
+messageSends: ["assert:equals:", "first:", "collection", "copyFrom:to:", "should:raise:"],
+referencedClasses: ["Error"]
+}),
+smalltalk.SequenceableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testFourth",
@@ -2800,6 +2829,43 @@ referencedClasses: []
 }),
 smalltalk.SequenceableCollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testLastN",
+protocol: 'tests',
+fn: function (){
+var self=this;
+function $Error(){return smalltalk.Error||(typeof Error=="undefined"?nil:Error)}
+return smalltalk.withContext(function($ctx1) { 
+var $2,$1,$4,$7,$6,$5,$9,$8,$3;
+$2=self._collection();
+$ctx1.sendIdx["collection"]=1;
+$1=_st($2)._last_((2));
+$ctx1.sendIdx["last:"]=1;
+$4=self._collection();
+$ctx1.sendIdx["collection"]=2;
+$7=self._collection();
+$ctx1.sendIdx["collection"]=3;
+$6=_st($7)._size();
+$ctx1.sendIdx["size"]=1;
+$5=_st($6).__minus((2));
+$9=self._collection();
+$ctx1.sendIdx["collection"]=4;
+$8=_st($9)._size();
+$3=_st($4)._copyFrom_to_($5,$8);
+self._assert_equals_($1,$3);
+self._should_raise_((function(){
+return smalltalk.withContext(function($ctx2) {
+return _st(self._collection())._last_((33));
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}),$Error());
+return self}, function($ctx1) {$ctx1.fill(self,"testLastN",{},smalltalk.SequenceableCollectionTest)})},
+args: [],
+source: "testLastN\x0a\x09self \x0a\x09\x09assert: (self collection last: 2) \x0a\x09\x09equals: (self collection copyFrom: self collection size -2 to: self collection size).\x0a\x09\x09\x0a\x09self should: [ self collection last: 33 ] raise: Error",
+messageSends: ["assert:equals:", "last:", "collection", "copyFrom:to:", "-", "size", "should:raise:"],
+referencedClasses: ["Error"]
+}),
+smalltalk.SequenceableCollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testSecond",

+ 15 - 4
st/Kernel-Collections.st

@@ -821,11 +821,13 @@ first
 	^ self at: 1
 !
 
-first: n
-	"Answer the first n elements of the receiver.
-	Raise an error if there are not enough elements."
+first: aNumber
+	"Answer the first `aNumber` elements of the receiver.
+	Raise an error if there are not enough elements in the receiver."
 
-	^ self copyFrom: 1 to: n
+	self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
+
+	^ self copyFrom: 1 to: aNumber
 !
 
 fourth
@@ -863,6 +865,15 @@ last
 	^ self at: self size
 !
 
+last: aNumber
+	"Answer the last aNumber elements of the receiver.
+	Raise an error if there are not enough elements in the receiver."
+
+	self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
+
+	^ self copyFrom: self size - aNumber to: self size
+!
+
 second
 	^ self at: 2
 !

+ 16 - 0
st/Kernel-Tests.st

@@ -846,6 +846,14 @@ testFirst
 	self assert: (self collection first) equals: (self collection at: 1)
 !
 
+testFirstN
+	self 
+		assert: (self collection first: 2) 
+		equals: (self collection copyFrom: 0 to: 2).
+		
+	self should: [ self collection first: 33 ] raise: Error
+!
+
 testFourth
 	self assert: (self collection fourth) equals: (self collection at: 4)
 !
@@ -854,6 +862,14 @@ testLast
 	self assert: (self collection last) equals: (self collection at: self collection size)
 !
 
+testLastN
+	self 
+		assert: (self collection last: 2) 
+		equals: (self collection copyFrom: self collection size -2 to: self collection size).
+		
+	self should: [ self collection last: 33 ] raise: Error
+!
+
 testSecond
 	self assert: (self collection second) equals: (self collection at: 2)
 !