Unary - Binary message

Gary McGovern garywork at lineone.net
Fri Oct 26 00:45:22 UTC 2001


----- Original Message -----
From: "Richard A. O'Keefe" <ok at atlas.otago.ac.nz>
To: <squeak-dev at lists.squeakfoundation.org>
Sent: Thursday, October 25, 2001 6:13 AM
Subject: Re: Unary - Binary message


> "Gary McGovern" <garywork at lineone.net> wrote:
>     [Alan Key wrote:
> A way around this is to make a class to hold numbers that can be
> incremented (we used to call this a "gauge").  If you stick one
> of these in a variable slot, then all will work pretty well.
> Can you see where you should subclass gauge?
>     ]
> My guess would be make an IntegerArray with the index changing on
> incrementing. Close ?
>
> Perhaps a simpler example is a Counter.
>
> Create a new subclass Counter of Object with one instance variable
'value'.
> Give it the methods
>
>     value                         "access method"
> value ifNil: [value := 0] "lazy initialisation to 0".
> ^value
>
>     increment                     "bump value, return self"
>         value := self value + 1
>
>     decrementIfPositive
>         value > 0 ifTrue: [value := self value - 1]
>
>     isZero
>         ^self value = 0
>
> Now you can do
>     |c|
>     c := Counter new.
>     c value "==> 0"
>     c isZero "==> true"
>     c increment.
>     c isZero "==> false"
>     c decrementIfPositive.
>     c decrementIfPositive.
>     c value "==> 0"
>
> You don't want a counter that clamps at 0?  Then roll your own.
> (Note that ++ in C++ and C99 will clamp for one type but not others.)
> You want a method to set a counter to any number?  Add one.

Thanks Richard,
I ended up doing something similar. But I don't think it solves the problem
so much. The class I wrote was a subclass of Object but worked similar to a
writeStream (writing ints to an OrderedCollection) using a position
variable.

What I wanted was to overcome
count := count + 1
in iterations, (something I'm tired of already even as a beginner) and just
use
count ++

But my solution ends up with just as much typing.

count := Counter from: 1
count increment

>From: "Bijan Parsia" <bparsia at email.unc.edu>
> No pelting, but I will ask "why?" Richard & Alan already provided loads of
> other stuff, but I have no idea if it meets your needs, mainly because I
> don't know what your need *are* :)
>
> I guess now that I've written some Python 2.0, I have used a language with
> "increment". But usually, if I want to do something like that, I do ye old
> a := a+1
>
> Or some such.
>
> When you mentioned Integer arrays, I began to wonder if you were using it
> to step over an array...there are lots better techniques. (E.g., if you
> use a stream over the array you can send #next messages to your heart's
> content or #atEnd. If you are processing the items in a collection, then
> #do, #collect:, etc. are your friends.)
>
> If you're just trying to write an incrementer, then Richard's example will
> do. If you're writing it for some purpose, maybe a few details will pull
> out a better way to do it.

Thanks Bijan,
I think I've gone hybrid between you and Richard, but I doubt I've gone
close to what Alan was talking about. One day he may learn to spoon feed the
knowledge (but on the good side I know a lot more about the number classes).

Regards,
Gary







More information about the Squeak-dev mailing list