[Newbies] Total newb...

Matthias Berth matthias.berth at googlemail.com
Tue Oct 14 07:33:03 UTC 2008


Hi Tony,


> But what I really was looking for is
>
> Given a set, how do I determine
>
> 1) If an object is a member of he set.

use aSet includes: anElement. As you noted, that requires equality to work.

> 2) How do I specify when two objects are equal? Is part of this.

In Smalltalk, you use =  where you would use equals() in Java. The
same rule applies: if you override = you sould also override the
method hash.

You can look at the implementation of includes: in class Collection:

  includes: anObject
	"Answer whether anObject is one of the receiver's elements."

	^ self anySatisfy: [:each | each = anObject]

That confirms that = is the important method :-)


Cheers

Matthias


> Obviously two objects which have the same address which I think in smalltalk
> is == operator are the same object..
>
> but what if I want equality to be based on the value of some internal class
> value.
>
> Maybe I'm thinking too much like a java programmer.
>
> Tony
>
> On Oct 14, 2008, at 2:56 AM, Matthias Berth wrote:
>
>> Hi Michael,
>>
>> I think Tonys model of the game is  like this:
>>
>> A Player throws a hand. That can be any of "rock", "scissors", or
>> "paper". Now someone (a Game object?) has to check if the player did
>> not make a mistake, say by throwing a "well". The set of legal throws
>> (in this game, anyway) is somehow defined by Tony. So the Game object
>> checks if the throw is an element of the set of legal throws.
>>
>> Am I describing this correctly, Tony?
>>
>> Cheers
>>
>> Matthias
>>
>> On Tue, Oct 14, 2008 at 8:45 AM, Michael Haupt <mhaupt at gmail.com> wrote:
>>>
>>> Hi Tony,
>>>
>>> On Tue, Oct 14, 2008 at 8:11 AM, Tony Giaccone <tony at giaccone.org> wrote:
>>>>
>>>> validHands := Set new.
>>>> validHands add: Rock new; add Paper new; add Scissors  new.
>>>>
>>>> Assume I have a player object which responds to the method  throwsAHand
>>>> with
>>>> an instance of Rock Paper or Scissors.
>>>>
>>>> how do I craft
>>>>
>>>> validHands contains: aPlayer throwsAHand
>>>
>>> 'ere, how about this:
>>>
>>> validHands anySatisfy: [ :elem | elem respondsTo: #throwsAHand ]
>>>
>>> Collection >> #anySatisfy: takes a block and evaluates it for all the
>>> elements in the collection. It returns true if the block evaluates to
>>> true for any of the elements, and false otherwise.
>>>
>>> Object >> #respondsTo: accepts a symbol (!) denoting a message name
>>> and returns true if the object in question understands that message.
>>>
>>> Did I make clear what the above code does?
>>>
>>> Best,
>>>
>>> Michael
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at lists.squeakfoundation.org
>>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at lists.squeakfoundation.org
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list