Total newb...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Total newb...

Tony Giaccone-2
Ok, so I'm really new to smalltalk. I've done a few basic tutorials  
and have a simple understanding of the syntax. My pervious programing  
experience is mostly java/C with a bit of Objective C in the mix.

I'm trying to figure out how to do what seems like a simple thing.

I have a set, I'd like to find out if an object exists in the set.

In a general form. Let's use the a relatively simple case.

Assume I have classes Rock Paper and Scissors.


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

I know that contains: takes a block, and that this isn't correctly  
done.. but I'm trying to get the a handle on how to do this.
The intent is to return a boolean, that indicates if the object the  
player threw is in the Set of valid objects that can be thrown.


Tony


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Total newb...

Matthias Berth-2
Hi Tony,

welcome to Smalltalk :-) Try this:

s := 'abcdef' asSet.
s includes: $a.

To see similar methods, browse the class Collection - in the testing category.

HTH

Matthias

On Tue, Oct 14, 2008 at 8:11 AM, Tony Giaccone <[hidden email]> wrote:

> Ok, so I'm really new to smalltalk. I've done a few basic tutorials and have
> a simple understanding of the syntax. My pervious programing experience is
> mostly java/C with a bit of Objective C in the mix.
>
> I'm trying to figure out how to do what seems like a simple thing.
>
> I have a set, I'd like to find out if an object exists in the set.
>
> In a general form. Let's use the a relatively simple case.
>
> Assume I have classes Rock Paper and Scissors.
>
>
> 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
>
> I know that contains: takes a block, and that this isn't correctly done..
> but I'm trying to get the a handle on how to do this.
> The intent is to return a boolean, that indicates if the object the player
> threw is in the Set of valid objects that can be thrown.
>
>
> Tony
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Total newb...

Michael Haupt-3
In reply to this post by Tony Giaccone-2
Hi Tony,

On Tue, Oct 14, 2008 at 8:11 AM, Tony Giaccone <[hidden email]> 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Total newb...

Matthias Berth-2
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 <[hidden email]> wrote:

> Hi Tony,
>
> On Tue, Oct 14, 2008 at 8:11 AM, Tony Giaccone <[hidden email]> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Total newb...

Michael Haupt-3
Hi Matthias,

I don't know. Maybe the question was a bit unspecific, or I was a bit
too tired (never try to answer questions before the first cup of
coffee). Looking at his e-mail again, I think you're right. But he
might pick up something interesting from my gibberish anyway. :-)

Best,

Michael
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Total newb...

Tony Giaccone-2
In reply to this post by Matthias Berth-2
Exactly Correct.

There exists a method on Set, with selector contains: which would on  
first examination seem to be the right thing.

Set contains: object

but contains seems to take a Block not an object.

so perhaps includes does what I want?

Clearly this works

>> validHands anySatisfy: [ :elem | elem respondsTo: #throwsAHand ]

in that if the class has a that selector it would be correct.

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.

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

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 <[hidden email]>  
> wrote:
>> Hi Tony,
>>
>> On Tue, Oct 14, 2008 at 8:11 AM, Tony Giaccone <[hidden email]>  
>> 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
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Total newb...

Matthias Berth-2
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 <[hidden email]> wrote:
>>>
>>> Hi Tony,
>>>
>>> On Tue, Oct 14, 2008 at 8:11 AM, Tony Giaccone <[hidden email]> 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
>>> [hidden email]
>>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>>
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Total newb...

Ron Teitelbaum
In reply to this post by Tony Giaccone-2
Hi Tony,

You stumbled on one of the most powerful features of Smalltalk.  The Block
(See BlockContext).  Blocks are a contextual memory space.  They can be
passed around and do all sorts of great things that Smalltalk programmers
take for granted.

The basic form is [] this is a no argument, no code block.  Pretty boring
cause it does nothing.

A more advanced form is ['hello']  which is a block with a literal string.
Still pretty boring.  But at least you can get the string out of the block
by   aBlock := ['hello'].  ^aBlock value.

A bit more advanced: [:arg | 'Hello ', arg]  has an argument.
Now you can do ^aBlock value: 'Ron'.

You can have more arguments [:arg1 :arg2 | 'Hello ', arg1, ' ', arg2].

Now you can do ^aBlock value: self firstName value: self lastName.

Even more complicated is:
| isLoggedIn |

isLoggedIn := true.

[:arg | 'Hello ', arg, ' you are ', (isLoggedIn ifTrue: [''] ifFalse: ['
not']), ' logged in']

Now you can do ^aBlock value: 'Ron'. From anywhere and the block remembers
the context from where it was created.  Pretty cool huh.

The regular select uses a block too:

self select: [:anItem | anItem isBlue]

which uses a do that uses a block

self do: [:anElement |
        aBlock value: anElement) ifTrue ...
]

Blocks are certainly a good thing to learn.

Happy Coding,
Ron Teitelbaum

> -----Original Message-----
> From: [hidden email] [mailto:beginners-
> [hidden email]] On Behalf Of Tony Giaccone
> Sent: Tuesday, October 14, 2008 2:12 AM
> To: [hidden email]
> Subject: [Newbies] Total newb...
>
> Ok, so I'm really new to smalltalk. I've done a few basic tutorials
> and have a simple understanding of the syntax. My pervious programing
> experience is mostly java/C with a bit of Objective C in the mix.
>
> I'm trying to figure out how to do what seems like a simple thing.
>
> I have a set, I'd like to find out if an object exists in the set.
>
> In a general form. Let's use the a relatively simple case.
>
> Assume I have classes Rock Paper and Scissors.
>
>
> 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
>
> I know that contains: takes a block, and that this isn't correctly
> done.. but I'm trying to get the a handle on how to do this.
> The intent is to return a boolean, that indicates if the object the
> player threw is in the Set of valid objects that can be thrown.
>
>
> Tony
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Total newb...

Marcin Tustin
For C programmers, another way to look at blocks is that it is like passing a function pointer, except that instead of specifying the address of a function that you wrote elsewhere, you just write the text of the function.

On 10/14/08, Ron Teitelbaum <[hidden email]> wrote:
Hi Tony,

You stumbled on one of the most powerful features of Smalltalk.  The Block
(See BlockContext).  Blocks are a contextual memory space.  They can be
passed around and do all sorts of great things that Smalltalk programmers
take for granted.

The basic form is [] this is a no argument, no code block.  Pretty boring
cause it does nothing.

A more advanced form is ['hello']  which is a block with a literal string.
Still pretty boring.  But at least you can get the string out of the block
by   aBlock := ['hello'].  ^aBlock value.

A bit more advanced: [:arg | 'Hello ', arg]  has an argument.
Now you can do ^aBlock value: 'Ron'.

You can have more arguments [:arg1 :arg2 | 'Hello ', arg1, ' ', arg2].

Now you can do ^aBlock value: self firstName value: self lastName.

Even more complicated is:
| isLoggedIn |

isLoggedIn := true.

[:arg | 'Hello ', arg, ' you are ', (isLoggedIn ifTrue: [''] ifFalse: ['
not']), ' logged in']

Now you can do ^aBlock value: 'Ron'. From anywhere and the block remembers
the context from where it was created.  Pretty cool huh.

The regular select uses a block too:

self select: [:anItem | anItem isBlue]

which uses a do that uses a block

self do: [:anElement |
       aBlock value: anElement) ifTrue ...
]

Blocks are certainly a good thing to learn.

Happy Coding,
Ron Teitelbaum

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]
> [hidden email]] On Behalf Of Tony Giaccone
> Sent: Tuesday, October 14, 2008 2:12 AM
> To: [hidden email]
> Subject: [Newbies] Total newb...
>
> Ok, so I'm really new to smalltalk. I've done a few basic tutorials
> and have a simple understanding of the syntax. My pervious programing
> experience is mostly java/C with a bit of Objective C in the mix.
>
> I'm trying to figure out how to do what seems like a simple thing.
>
> I have a set, I'd like to find out if an object exists in the set.
>
> In a general form. Let's use the a relatively simple case.
>
> Assume I have classes Rock Paper and Scissors.
>
>
> 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
>
> I know that contains: takes a block, and that this isn't correctly
> done.. but I'm trying to get the a handle on how to do this.
> The intent is to return a boolean, that indicates if the object the
> player threw is in the Set of valid objects that can be thrown.
>
>
> Tony
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners