Hello
First of all. The creation of this mailinglist was a very good idea. Reading the original list I always felt wrong, because of the very high level of questions and ... yes answers. Thanks :)
Anyway. I was looking for a crossSum Method for Integers.
e.g. 251 crossSum (sometimes called checksum) should return 8
1234 -> 10
I couldnt find one with method finder. So I wrote one by myself. And a friend - also a smalltalk newbie - did another one.
Probably both are not perfect.
The first problem is: Where to put the new method? We decide to put it directly into Integer (arithmetic). There are no side effects, we guess. Are we right?
Second, we dont know the class library very well. It tooks 20 min to find the smalltalk equivalent to !=
Its ~= for other newbies :)
So we never know, if we are doing it the smalltalk way.
It would be great if others post their solution and it could be a good example for describing the test first development cycle, too. Another area I am not so familiar with. But I am eager to learn this.
BTW: How can I tell method finder to use new implemented methods for example search? 1. 2. 3. 4. 10 didnt show me crossSum.
Have a nice day Enno
Enrico Schwass a écrit :
Second, we dont know the class library very well. It tooks 20 min to find the smalltalk equivalent to !=
Its ~= for other newbies :)
A bit off-topic, did you take a look at the following document: http://minnow.cc.gatech.edu/squeak/uploads/SqueakClassesRef.html
Hilaire
Enrico Schwass wrote:
Hello
First of all. The creation of this mailinglist was a very good idea. Reading the original list I always felt wrong, because of the very high level of questions and ... yes answers. Thanks :)
Anyway. I was looking for a crossSum Method for Integers.
e.g. 251 crossSum (sometimes called checksum) should return 8
1234 -> 10
I couldnt find one with method finder. So I wrote one by myself. And a friend - also a smalltalk newbie - did another one.
Probably both are not perfect.
The first problem is: Where to put the new method? We decide to put it directly into Integer (arithmetic). There are no side effects, we guess. Are we right?
Yes. At some point (or maybe already) you might wonder how you can maintain such class extensions. If you are using ChangeSet's then it is no problem, your new method is automatically added to your current CS. If you are using Monticello (recommended once you "really get going") then you need to use a method category which corresponds to your package name. For example, suppose you create a Monticello package named "MyProject" then you should put your extension to Integer into a method category named "*MyProject" (case insensitive) so that Monticello knows that it is part of the package.
Second, we dont know the class library very well. It tooks 20 min to find the smalltalk equivalent to !=
Its ~= for other newbies :)
Yes, ouch. A good Smalltalk book would help you a lot. Have a look at:
http://www.iam.unibe.ch/~ducasse/FreeBooks.html
Most of these books are quite good and they will almost all introduce you to the class library basics. I like Sharp's book even if it has a VisualWorks Smalltalk slant. "Smalltalk with Style" is more about how you should write code in Smalltalk rather than the Smalltalk language itself so you might save it for your second book.
David
Enrico Schwass wrote:
Hello
First of all. The creation of this mailinglist was a very good idea. Reading the original list I always felt wrong, because of the very high level of questions and ... yes answers. Thanks :)
Anyway. I was looking for a crossSum Method for Integers.
e.g. 251 crossSum (sometimes called checksum) should return 8
1234 -> 10
I couldnt find one with method finder. So I wrote one by myself. And a friend - also a smalltalk newbie - did another one.
Probably both are not perfect.
The first problem is: Where to put the new method? We decide to put it directly into Integer (arithmetic). There are no side effects, we guess. Are we right?
Second, we dont know the class library very well. It tooks 20 min to find the smalltalk equivalent to !=
Its ~= for other newbies :)
So we never know, if we are doing it the smalltalk way.
It would be great if others post their solution and it could be a good example for describing the test first development cycle, too. Another area I am not so familiar with. But I am eager to learn this.
BTW: How can I tell method finder to use new implemented methods for example search? 1. 2. 3. 4. 10 didnt show me crossSum.
Have a nice day Enno
Hi!
As a not-so-long-user of squeak myself, I've always found this page invaluable for digging out various fundamental language features:
http://minnow.cc.gatech.edu/squeak/5699
Sorry if you've seen it before, or seen it mentioned before ;) HTH. Simon
From: Enrico Schwass Sent: Tuesday, May 09, 2006 6:30 AM
e.g. 251 crossSum (sometimes called checksum) should return 8
1234 -> 10
The first problem is: Where to put the new method? We decide to put it directly into Integer (arithmetic). There are no side effects, we guess. Are we right?
The first question is a very good question. The answer is, "It depends". It depends on a lot of factors like how you are using it and if the method has a more general purpose. What I usually do is try to figure out where the method is going to be used. As a rule of thumb if there is no self in your method it should either be a class method in really intuitive spot, or it should be moved to where the self is. So to answer your question, if your method has a specific purpose on a class, having a class method like: VerifiedMessage class>> crossSum: anInteger makes sense. If you believe that it is useful for consumers of integer then Integer >> crossSum makes sense.
It would be great if others post their solution and it could be a good example for describing the test first development cycle, too.
Which would give you:
VerifiedMessage class>> crossSum: anInteger "return to the sender the sum of each decimal digit of anInteger" ^anInteger asString inject: 0 into: [:a :b | a + b asString asInteger]
Since the method is in your own class the testing is much simpler, but if the method was in integer, then it's important to make sure you are not overriding another implementation of crossSum: and that your method works on all subclasses of Integer.
But it should work
Integer>> crossSum "return to the sender the sum of each decimal digit of self" ^self asString detectSum: [:a | a asString asInteger]
Ron Teitelbaum President / Principal Software Engineer US Medical Record Specialists Ron@USMedRec.com
What I suggest also is to write SUnits to document the behavior of your program. You will see that when the bar of the test runner will be green after a large change, you will get a really powerful feeling.
I wrote the chapter Sunit Explained only for people starting. In 3.9 there is a reallllllly cool SUnit browser.
Stef
On 9 mai 06, at 12:30, Enrico Schwass wrote:
Hello
First of all. The creation of this mailinglist was a very good idea. Reading the original list I always felt wrong, because of the very high level of questions and ... yes answers. Thanks :)
Anyway. I was looking for a crossSum Method for Integers.
e.g. 251 crossSum (sometimes called checksum) should return 8
1234 -> 10
I couldnt find one with method finder. So I wrote one by myself. And a friend - also a smalltalk newbie - did another one.
Probably both are not perfect.
The first problem is: Where to put the new method? We decide to put it directly into Integer (arithmetic). There are no side effects, we guess. Are we right?
Second, we dont know the class library very well. It tooks 20 min to find the smalltalk equivalent to !=
Its ~= for other newbies :)
So we never know, if we are doing it the smalltalk way.
It would be great if others post their solution and it could be a good example for describing the test first development cycle, too. Another area I am not so familiar with. But I am eager to learn this.
BTW: How can I tell method finder to use new implemented methods for example search? 1. 2. 3. 4. 10 didnt show me crossSum.
Have a nice day Enno
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Hi,
crossSum again...
Here my implementation that now even runs for negative numbers... ;-) - and comes with pre-/postconditions and some tests.
I posted it also on Mantis, so maybe it might get even included in an image >= 3.9 at sometime.
Cheers,
Markus
p.s. I also implemented an example to visually compare the outcome with the colormap from http://mathworld.wolfram.com/DigitSum.html (and run all the pre- and post-conditions on the way...)
| aForm aWheel maxNumber maxBase | maxNumber := 100. maxBase := 100. aForm := Form extent: (maxBase+1) @ (maxNumber+1) depth: Display depth. aWheel := Color wheel: ((maxNumber ln / maxBase ln) * maxBase) saturation: 1.0 brightness: 1.0. 2 to: maxBase do: [:aBase | 1 to: maxNumber do: [:aNumber | aForm colorAt: aNumber @ (maxBase - aBase) put: (aWheel at: (aNumber crossSumBase: aBase))]]. (aForm scaledToSize: 280@280) display
Resulting pic of this code looks like:
Legend: As I understand the mathematica legend, the base should the x, and the number the y.
Bizarre thing is, that I had to swap x and y coordinates to reproduce the result, anybody any ideas why? I assume that Form >> colorAt: x@y put: aColor is the way to use Forms and not Form >> colorAt: y@x put: aColor
So is it me or Stephen Wolfram doing a mistake here? ;-)
On May 10, 2006, at 11:00 AM, stéphane ducasse wrote:
What I suggest also is to write SUnits to document the behavior of your program. You will see that when the bar of the test runner will be green after a large change, you will get a really powerful feeling.
I wrote the chapter Sunit Explained only for people starting. In 3.9 there is a reallllllly cool SUnit browser.
Stef
On 9 mai 06, at 12:30, Enrico Schwass wrote:
Hello
First of all. The creation of this mailinglist was a very good idea. Reading the original list I always felt wrong, because of the very high level of questions and ... yes answers. Thanks :)
Anyway. I was looking for a crossSum Method for Integers.
e.g. 251 crossSum (sometimes called checksum) should return 8
1234 -> 10
I couldnt find one with method finder. So I wrote one by myself. And a friend - also a smalltalk newbie - did another one.
Probably both are not perfect.
The first problem is: Where to put the new method? We decide to put it directly into Integer (arithmetic). There are no side effects, we guess. Are we right?
Second, we dont know the class library very well. It tooks 20 min to find the smalltalk equivalent to !=
Its ~= for other newbies :)
So we never know, if we are doing it the smalltalk way.
It would be great if others post their solution and it could be a good example for describing the test first development cycle, too. Another area I am not so familiar with. But I am eager to learn this.
BTW: How can I tell method finder to use new implemented methods for example search? 1. 2. 3. 4. 10 didnt show me crossSum.
Have a nice day Enno
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org