Browse Source

Improved class comment for Random, added class comment for Point. Added a few testing methods in Number and a printString implementation in Point.

Göran Krampe 12 years ago
parent
commit
d11d90ceab
3 changed files with 170 additions and 5 deletions
  1. 44 0
      js/Kernel-Objects.deploy.js
  2. 66 1
      js/Kernel-Objects.js
  3. 60 4
      st/Kernel-Objects.st

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

@@ -1620,6 +1620,39 @@ return self;}
 }),
 smalltalk.Number);
 
+smalltalk.addMethod(
+'_negative',
+smalltalk.method({
+selector: 'negative',
+fn: function (){
+var self=this;
+return self < (0);
+return self;}
+}),
+smalltalk.Number);
+
+smalltalk.addMethod(
+'_positive',
+smalltalk.method({
+selector: 'positive',
+fn: function (){
+var self=this;
+return self >= (0);
+return self;}
+}),
+smalltalk.Number);
+
+smalltalk.addMethod(
+'_isZero',
+smalltalk.method({
+selector: 'isZero',
+fn: function (){
+var self=this;
+return smalltalk.send(self, "__eq", [(0)]);
+return self;}
+}),
+smalltalk.Number);
+
 
 smalltalk.addMethod(
 '_pi',
@@ -2528,6 +2561,17 @@ return self;}
 }),
 smalltalk.Point);
 
+smalltalk.addMethod(
+'_printString',
+smalltalk.method({
+selector: 'printString',
+fn: function (){
+var self=this;
+return smalltalk.send((smalltalk.String || String), "_streamContents_", [(function(stream){smalltalk.send(stream, "_nextPutAll_", [smalltalk.send(smalltalk.send(self['@x'], "_printString", []), "__comma", [unescape("@")])]);((($receiver = smalltalk.send(smalltalk.send(self['@y'], "_notNil", []), "_and_", [(function(){return smalltalk.send(self['@y'], "_negative", []);})])).klass === smalltalk.Boolean) ? ($receiver ? (function(){return smalltalk.send(stream, "_space", []);})() : nil) : smalltalk.send($receiver, "_ifTrue_", [(function(){return smalltalk.send(stream, "_space", []);})]));return smalltalk.send(stream, "_nextPutAll_", [smalltalk.send(self['@y'], "_printString", [])]);})]);
+return self;}
+}),
+smalltalk.Point);
+
 
 smalltalk.addMethod(
 '_x_y_',

+ 66 - 1
js/Kernel-Objects.js

@@ -2312,6 +2312,54 @@ referencedClasses: []
 }),
 smalltalk.Number);
 
+smalltalk.addMethod(
+unescape('_negative'),
+smalltalk.method({
+selector: unescape('negative'),
+category: 'testing',
+fn: function (){
+var self=this;
+return self < (0);
+return self;},
+args: [],
+source: unescape('negative%0A%09%22Answer%20whether%20the%20receiver%20is%20mathematically%20negative.%22%0A%0A%09%5E%20self%20%3C%200'),
+messageSends: [unescape("%3C")],
+referencedClasses: []
+}),
+smalltalk.Number);
+
+smalltalk.addMethod(
+unescape('_positive'),
+smalltalk.method({
+selector: unescape('positive'),
+category: 'testing',
+fn: function (){
+var self=this;
+return self >= (0);
+return self;},
+args: [],
+source: unescape('positive%0A%09%22Answer%20whether%20the%20receiver%20is%20positive%20or%20equal%20to%200.%20%28ST-80%20protocol%29.%22%0A%0A%09%5E%20self%20%3E%3D%200'),
+messageSends: [unescape("%3E%3D")],
+referencedClasses: []
+}),
+smalltalk.Number);
+
+smalltalk.addMethod(
+unescape('_isZero'),
+smalltalk.method({
+selector: unescape('isZero'),
+category: 'testing',
+fn: function (){
+var self=this;
+return smalltalk.send(self, "__eq", [(0)]);
+return self;},
+args: [],
+source: unescape('isZero%0A%09%5Eself%20%3D%200'),
+messageSends: [unescape("%3D")],
+referencedClasses: []
+}),
+smalltalk.Number);
+
 
 smalltalk.addMethod(
 unescape('_pi'),
@@ -3423,7 +3471,7 @@ smalltalk.UndefinedObject.klass);
 
 
 smalltalk.addClass('Random', smalltalk.Object, [], 'Kernel-Objects');
-smalltalk.Random.comment=unescape('Random%20is%20just%20a%20wrapper%20around%20javascript%20Math.random%28%29%20and%20is%20trivially%20used%20like%20this%3A%0A%0A%09Random%20new%20next%0A%0AThis%20will%20return%20a%20float%20x%20where%20x%20%3C%201%20and%20x%20%3E%200.%20If%20you%20want%20a%20random%20integer%20between%201%20and%2010%20you%20can%20use%20%23atRandom%0A%0A%0910%20atRandom%0A%0A...which%20is%20also%20implemented%20in%20SequencableCollection%20so%20you%20can%20easy%20pick%20an%20element%20at%20random%3A%0A%0A%09%23%28%27a%27%20%27b%27%20%27c%27%29%20atRandom')
+smalltalk.Random.comment=unescape('Random%20is%20a%20random%20number%20generator%20and%20is%20implemented%20as%20a%20wrapper%20around%20javascript%20Math.random%28%29%20and%20is%20trivially%20used%20like%20this%3A%0A%0A%09Random%20new%20next%0A%0AThis%20will%20return%20a%20float%20x%20where%20x%20%3C%201%20and%20x%20%3E%200.%20If%20you%20want%20a%20random%20integer%20from%201%20to%2010%20you%20can%20use%20%23atRandom%0A%0A%0910%20atRandom%0A%0A...and%20if%20you%20want%20a%20random%20number%20in%20a%20specific%20interval%20this%20also%20works%3A%0A%0A%09%283%20to%3A%207%29%20atRandom%0A%0A...but%20be%20aware%20that%20%23to%3A%20does%20not%20create%20an%20Interval%20as%20in%20other%20Smalltalks%20but%20in%20fact%20an%20Array%20of%20numbers%2C%20so%20it%27s%20better%20to%20use%3A%0A%0A%095%20atRandom%20+%202%0A%0ASince%20%23atRandom%20is%20implemented%20in%20SequencableCollection%20you%20can%20easy%20pick%20an%20element%20at%20random%3A%0A%0A%09%23%28%27a%27%20%27b%27%20%27c%27%29%20atRandom%0A%0A...or%20perhaps%20a%20letter%20from%20a%20String%3A%0A%0A%09%27abc%27%20atRandom')
 smalltalk.addMethod(
 unescape('_next'),
 smalltalk.method({
@@ -3459,6 +3507,7 @@ smalltalk.Random);
 
 
 smalltalk.addClass('Point', smalltalk.Object, ['x', 'y'], 'Kernel-Objects');
+smalltalk.Point.comment=unescape('A%20Point%20represents%20an%20x-y%20pair%20of%20numbers%20usually%20designating%20a%20geometric%20coordinate.%0APoints%20are%20traditionally%20created%20using%20the%20binary%20%23@%20message%20to%20a%20number%3A%0A%0A%09100@120%0A%0APoints%20can%20then%20be%20arithmetically%20manipulated%20using%20math%3A%0A%0A%09100@100%20+%20%2810@10%29%0A%0ACreating%20a%20Point%20with%20a%20negative%20y-value%20will%20need%20a%20space%20after%20@%20in%20order%20to%20avoid%20a%20parsing%20error%2C%0A10@%20-5%20works%20fine%2C%20but%20not%2010@-5%0A%0AAmber%20does%20not%20have%20much%20behavior%20in%20this%20class%20out-of-the-box.')
 smalltalk.addMethod(
 unescape('_x'),
 smalltalk.method({
@@ -3619,6 +3668,22 @@ referencedClasses: []
 }),
 smalltalk.Point);
 
+smalltalk.addMethod(
+unescape('_printString'),
+smalltalk.method({
+selector: unescape('printString'),
+category: 'printing',
+fn: function (){
+var self=this;
+return smalltalk.send((smalltalk.String || String), "_streamContents_", [(function(stream){smalltalk.send(stream, "_nextPutAll_", [smalltalk.send(smalltalk.send(self['@x'], "_printString", []), "__comma", [unescape("@")])]);((($receiver = smalltalk.send(smalltalk.send(self['@y'], "_notNil", []), "_and_", [(function(){return smalltalk.send(self['@y'], "_negative", []);})])).klass === smalltalk.Boolean) ? ($receiver ? (function(){return smalltalk.send(stream, "_space", []);})() : nil) : smalltalk.send($receiver, "_ifTrue_", [(function(){return smalltalk.send(stream, "_space", []);})]));return smalltalk.send(stream, "_nextPutAll_", [smalltalk.send(self['@y'], "_printString", [])]);})]);
+return self;},
+args: [],
+source: unescape('printString%0A%09%22Print%20receiver%20in%20classic%20x@y%20notation.%22%0A%0A%09%5EString%20streamContents%3A%20%5B%3Astream%20%7C%0A%09%09stream%20nextPutAll%3A%20x%20printString%2C%20%27@%27.%0A%09%09%28y%20notNil%20and%3A%20%5By%20negative%5D%29%0A%09%09%09ifTrue%3A%20%5B%0A%09%09%09%09%22Avoid%20ambiguous%20@-%20construct%22%0A%09%09%09%09stream%20space%5D.%0A%09%09stream%20nextPutAll%3A%20y%20printString%5D'),
+messageSends: ["streamContents:", "nextPutAll:", unescape("%2C"), "printString", "ifTrue:", "and:", "notNil", "negative", "space"],
+referencedClasses: ["String"]
+}),
+smalltalk.Point);
+
 
 smalltalk.addMethod(
 unescape('_x_y_'),

+ 60 - 4
st/Kernel-Objects.st

@@ -839,6 +839,22 @@ even
 
 odd
 	^ self even not
+!
+
+negative
+	"Answer whether the receiver is mathematically negative."
+
+	^ self < 0
+!
+
+positive
+	"Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
+
+	^ self >= 0
+!
+
+isZero
+	^self = 0
 ! !
 
 !Number methodsFor: 'timeouts/intervals'!
@@ -1219,17 +1235,29 @@ Object subclass: #Random
 	instanceVariableNames: ''
 	category: 'Kernel-Objects'!
 !Random commentStamp!
-Random is just a wrapper around javascript Math.random() and is trivially used like this:
+Random is a random number generator and is implemented as a wrapper around javascript Math.random() and is trivially used like this:
 
 	Random new next
 
-This will return a float x where x < 1 and x > 0. If you want a random integer between 1 and 10 you can use #atRandom
+This will return a float x where x < 1 and x > 0. If you want a random integer from 1 to 10 you can use #atRandom
 
 	10 atRandom
 
-...which is also implemented in SequencableCollection so you can easy pick an element at random:
+...and if you want a random number in a specific interval this also works:
+
+	(3 to: 7) atRandom
+
+...but be aware that #to: does not create an Interval as in other Smalltalks but in fact an Array of numbers, so it's better to use:
+
+	5 atRandom + 2
+
+Since #atRandom is implemented in SequencableCollection you can easy pick an element at random:
 
-	#('a' 'b' 'c') atRandom!
+	#('a' 'b' 'c') atRandom
+
+...or perhaps a letter from a String:
+
+	'abc' atRandom!
 
 !Random methodsFor: 'accessing'!
 
@@ -1244,6 +1272,20 @@ next: anInteger
 Object subclass: #Point
 	instanceVariableNames: 'x y'
 	category: 'Kernel-Objects'!
+!Point commentStamp!
+A Point represents an x-y pair of numbers usually designating a geometric coordinate.
+Points are traditionally created using the binary #@ message to a number:
+
+	100@120
+
+Points can then be arithmetically manipulated using math:
+
+	100@100 + (10@10)
+
+Creating a Point with a negative y-value will need a space after @ in order to avoid a parsing error,
+10@ -5 works fine, but not 10@-5
+
+Amber does not have much behavior in this class out-of-the-box.!
 
 !Point methodsFor: 'accessing'!
 
@@ -1292,6 +1334,20 @@ asPoint
 	^self
 ! !
 
+!Point methodsFor: 'printing'!
+
+printString
+	"Print receiver in classic x@y notation."
+
+	^String streamContents: [:stream |
+		stream nextPutAll: x printString, '@'.
+		(y notNil and: [y negative])
+			ifTrue: [
+				"Avoid ambiguous @- construct"
+				stream space].
+		stream nextPutAll: y printString]
+! !
+
 !Point class methodsFor: 'instance creation'!
 
 x: aNumber y: anotherNumber