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.<br>
<br>
<div><span class="gmail_quote">On 10/14/08, <b class="gmail_sendername">Ron Teitelbaum</b> <<a href="mailto:Ron@usmedrec.com">Ron@usmedrec.com</a>> wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi Tony,<br><br>You stumbled on one of the most powerful features of Smalltalk. The Block<br>(See BlockContext). Blocks are a contextual memory space. They can be<br>
passed around and do all sorts of great things that Smalltalk programmers<br>take for granted.<br><br>The basic form is [] this is a no argument, no code block. Pretty boring<br>cause it does nothing.<br><br>A more advanced form is ['hello'] which is a block with a literal string.<br>
Still pretty boring. But at least you can get the string out of the block<br>by aBlock := ['hello']. ^aBlock value.<br><br>A bit more advanced: [:arg | 'Hello ', arg] has an argument.<br>Now you can do ^aBlock value: 'Ron'.<br>
<br>You can have more arguments [:arg1 :arg2 | 'Hello ', arg1, ' ', arg2].<br><br>Now you can do ^aBlock value: self firstName value: self lastName.<br><br>Even more complicated is:<br>| isLoggedIn |<br><br>
isLoggedIn := true.<br><br>[:arg | 'Hello ', arg, ' you are ', (isLoggedIn ifTrue: [''] ifFalse: ['<br>not']), ' logged in']<br><br>Now you can do ^aBlock value: 'Ron'. From anywhere and the block remembers<br>
the context from where it was created. Pretty cool huh.<br><br>The regular select uses a block too:<br><br>self select: [:anItem | anItem isBlue]<br><br>which uses a do that uses a block<br><br>self do: [:anElement |<br>
aBlock value: anElement) ifTrue ...<br>]<br><br>Blocks are certainly a good thing to learn.<br><br>Happy Coding,<br>Ron Teitelbaum<br><br>> -----Original Message-----<br>> From: <a href="mailto:beginners-bounces@lists.squeakfoundation.org">beginners-bounces@lists.squeakfoundation.org</a> [mailto:<a href="mailto:beginners-">beginners-</a><br>
> <a href="mailto:bounces@lists.squeakfoundation.org">bounces@lists.squeakfoundation.org</a>] On Behalf Of Tony Giaccone<br>> Sent: Tuesday, October 14, 2008 2:12 AM<br>> To: <a href="mailto:beginners@lists.squeakfoundation.org">beginners@lists.squeakfoundation.org</a><br>
> Subject: [Newbies] Total newb...<br>><br>> Ok, so I'm really new to smalltalk. I've done a few basic tutorials<br>> and have a simple understanding of the syntax. My pervious programing<br>> experience is mostly java/C with a bit of Objective C in the mix.<br>
><br>> I'm trying to figure out how to do what seems like a simple thing.<br>><br>> I have a set, I'd like to find out if an object exists in the set.<br>><br>> In a general form. Let's use the a relatively simple case.<br>
><br>> Assume I have classes Rock Paper and Scissors.<br>><br>><br>> validHands := Set new.<br>> validHands add: Rock new; add Paper new; add Scissors new.<br>><br>> Assume I have a player object which responds to the method<br>
> throwsAHand with an instance of Rock Paper or Scissors.<br>><br>> how do I craft<br>><br>> validHands contains: aPlayer throwsAHand<br>><br>> I know that contains: takes a block, and that this isn't correctly<br>
> done.. but I'm trying to get the a handle on how to do this.<br>> The intent is to return a boolean, that indicates if the object the<br>> player threw is in the Set of valid objects that can be thrown.<br>
><br>><br>> Tony<br>><br>><br>> _______________________________________________<br>> Beginners mailing list<br>> <a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br>
> <a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br><br>_______________________________________________<br>Beginners mailing list<br>
<a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br><a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br>