I have a few questions about writing methods dealing with arrays....
Firstly, how would i make a method that returns true or false if the receiver is in the array. example 1 inArray: #(1 2 3 4) should return to true
Second, how would i make a method that will return the number of integers in the array different from the integer parameter
anArray howmany: 5 should return how many elements in the array are different from the number
thanks again.
"noobie" == noobie pntball650@aol.com writes:
noobie> I have a few questions about writing methods dealing with noobie> arrays....
Arrays are a kind of Collection. Smalltalk's collection classes have a very rich protocol. If you start with Collection in a Hierarchical Browser, you'll see a dozen classes that derive from Collection and are optimized for different things.
The key methods are #select:, #detect:, #reject:, #collect:, and #inject:into:. Learn those first, because your methods would be built trivially with them, and then could be placed at Collection, not at Array, since your problem is more general than Arrays. :)
--- On Fri, 4/23/10, noobie pntball650@aol.com wrote:
From: noobie pntball650@aol.com Subject: [Newbies] Array questions To: beginners@lists.squeakfoundation.org Date: Friday, April 23, 2010, 6:07 PM
I have a few questions about writing methods dealing with arrays....
Firstly, how would i make a method that returns true or false if the receiver is in the array. example 1 inArray: #(1 2 3 4) should return to true
inArray: theArray
^theArray includes: self .
Second, how would i make a method that will return the number of integers in the array different from the integer parameter
anArray howmany: 5 should return how many elements in the array are different from the number
howMany: anItem
^(self reject: [ :each | each = anItem ]) size .
change reject to select to get the number that are the item.
You are asking very elementry questions here. You may be ready to read some of the literature.
I highly recommend Kent Becks book on Smalltalk. Smalltalk Best Practice Patterns by Kent Beck e.g.: http://www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X/re...
thanks again.
On 4/23/2010 3:48 PM, Jerome Peace wrote:
Second, how would i make a method that will return the number of integers in the array different from the integer parameter
anArray howmany: 5 should return how many elements in the array are different from the number
howMany: anItem
^(self reject: [ :each | each = anItem ]) size .
Better:
howManyAreNot: anItem "Answer the number of elements different from anItem"
^self count:[:each| each ~= anItem]
Why is that better? 3 reasons: 1) The method has a comment. 2) The method name tells you what the method does - the spec said to answer the number of elements that are NOT the argument; the method name should reflect that. 3) It uses #count: instead of #reject: making it more compact.
Do note that 1) and 2) really go together. Having a method called #howMany: that returns how many items are NOT in the collection and no comment clarifying whether the method is wrongly named or simply buggy is really problematic.
Cheers, - Andreas
Hi Andreas,
+1.
Shows the difference between thinking about things off the cuff and thinking about things after years of experience,
Though if I were programming things I would do it by using methods on hand rather than invent the new ones for this. We got to get him to read the books. Then the code.
Cheers --Jer
--- On Fri, 4/23/10, Andreas Raab andreas.raab@gmx.de wrote:
From: Andreas Raab andreas.raab@gmx.de Subject: [Newbies] Re: Array questions To: "A friendly place to get answers to even the most basic questions about Squeak." beginners@lists.squeakfoundation.org Date: Friday, April 23, 2010, 8:03 PM On 4/23/2010 3:48 PM, Jerome Peace wrote:
Second, how would i make a method that will return
the
number of integers in the array different from the integer parameter
anArray howmany: 5 should return
how many
elements in the array are different from the number
howMany: anItem
^(self reject: [ :each | each = anItem ]) size .
Better:
howManyAreNot: anItem "Answer the number of elements different from anItem"
^self count:[:each| each ~= anItem]
Why is that better? 3 reasons:
- The method has a comment.
- The method name tells you what the method does - the
spec said to answer the number of elements that are NOT the argument; the method name should reflect that. 3) It uses #count: instead of #reject: making it more compact.
Do note that 1) and 2) really go together. Having a method called #howMany: that returns how many items are NOT in the collection and no comment clarifying whether the method is wrongly named or simply buggy is really problematic.
Cheers, - Andreas
beginners@lists.squeakfoundation.org