Back: Iteration Up: Tutorial Forward: Integer loops   Top: GNU Smalltalk User's Guide Contents: Table of Contents Index: About: About this document

6.7 Code blocks, part two

In the last chapter, we looked at how code blocks could be used to build conditional expressions, and how you could iterate across all entries in a collection.(34) We built our own code blocks, and handed them off for use by system objects. But there is nothing magic about invoking code blocks; your own code will often need to do so. This chapter will shows some examples of loop construction in Smalltalk, and then demonstrate how you invoke code blocks for yourself.

6.7.1 Integer loops  Well, Smalltalk too has them
6.7.2 Intervals  And of course here's a peculiar way to use them
6.7.3 Invoking code blocks  You can do it, too


6.7.1 Integer loops

Integer loops are constructed by telling a number to drive the loop. Try this example to count from 1 to 20:
 
   1 to: 20 do: [:x | x printNl ]

There's also a way to count up by more than one:
 
   1 to: 20 by: 2 do: [:x | x printNl ]

Finally, counting down is done with a negative step:
 
   20 to: 1 by: -1 do: [:x | x printNl ]

Note that the x variable is local to the block.
 
   x
just prints nil.


6.7.2 Intervals

It is also possible to represent a range of numbers as a standalone object. This allows you to represent a range of numbers as a single object, which can be passed around the system.
 
   i := Interval from: 5 to: 10
   i do: [:x | x printNl]

As with the integer loops, the Interval class can also represent steps greater than 1. It is done much like it was for our numeric loop above:
 
   i := (Interval from: 5 to: 10 by: 2)
   i do: [:x| x printNl]


6.7.3 Invoking code blocks

Let us revisit the checking example and add a method for scanning only checks over a certain amount. This would allow our user to find "big" checks, by passing in a value below which we will not invoke their function. We will invoke their code block with the check number as an argument ment; they can use our existing check: message to get the amount.

 
   Checking extend [
       checksOver: amount do: aBlock
           history keysAndValuesDo: [:key :value |
               (value > amount)
                      ifTrue: [aBlock value: key]
           ]
   ]

The structure of this loop is much like our printChecks message sage from chapter 6. However, in this case we consider each entry, and only invoke the supplied block if the check's value is greater than the specified amount. The line:

 
   ifTrue: [aBlock value: key]

invokes the user-supplied block, passing as an argument the key, which is the check number. The value: message, when received by a code block, causes the code block to execute. Code blocks take value, value:, value:value:, and value:value:value: messages, so you can pass from 0 to 3 arguments to a code block.(35)

You might find it puzzling that an association takes a value message, and so does a code block. Remember, each object can do its own thing with a message. A code block gets run when it receives a value message. An association merely returns the value part of its key/value pair. The fact that both take the same message is, in this case, coincidence.

Let's quickly set up a new checking account with $250 (wouldn't this be nice in real life?) and write a couple checks. Then we'll see if our new method does the job correctly:
 
   mycheck := Checking new.
   mycheck deposit: 250
   mycheck newChecks: 100 count: 40 
   mycheck writeCheck: 10 
   mycheck writeCheck: 52 
   mycheck writeCheck: 15 
   mycheck checksOver: 1 do: [:x | x printNl] 
   mycheck checksOver: 17 do: [:x | x printNl] 
   mycheck checksOver: 200 do: [:x | x printNl] 

We will finish this chapter with an alternative way of writing our checksOver: code. In this example, we will use the message select: to pick the checks which exceed our value, instead of doing the comparison ourselves. We can then invoke the new resulting collection against the user's code block.

 
   Checking extend [
       checksOver: amount do: aBlock [
           | chosen |
           chosen := history select: [:amt| amt > amount].
           chosen keysDo: aBlock
       ]
   ]

Note that extend will also overwrite methods. Try the same tests as above, they should yield the same result!



Back: Intervals Up: Code blocks (II) Forward: Debugging   Top: GNU Smalltalk User's Guide Contents: Table of Contents Index: About: About this document


This document was generated on May, 22 2008 using texi2html