Yet Another Case-statement Idea
Phil Weichert
weichert at hal-pc.org
Sun Jul 5 19:21:10 UTC 1998
Here is some examples from the code.
..
!Case class methodsFor: 'examples'!
enumerationTest: anInteger
"Answer true if anInteger is included in collection else answer false.
This can be combined with other caseIsAny:then: and case:then: statements to
test aValue to be one of a combination of discrete values or passed any of
several range tests."
"Case enumerationTest: 6."
"Case enumerationTest: 10."
^anInteger switch
caseIsAny: #( 1 3 6 12 16) then: [ true ];
default: [ false ].!
vendLargestAvailableCurrency: unVendedCurrenyValue
"Similiar to Baumann's MoneyVendor>>vendLargestAvailableCurrency but modified
to not require
additional classes or new methods. "
"Case vendLargestAvailableCurrency: 5"
"Case vendLargestAvailableCurrency: 50"
"Case vendLargestAvailableCurrency: 500"
| hasHundred hasFifty hasTwenty hasTen |
hasHundred := true. hasFifty := true. hasTwenty := true. hasTen := true.
^unVendedCurrenyValue switch
case: [:value | value >= 100 and: [hasHundred]]
then: ['vend hundred'];
case: [:value | value >= 50 and: [hasFifty]]
then: ['vend fifty'];
case: [:value | value >= 20 and: [hasTwenty]]
then: ['vend twenty'];
case: [:value | value >= 10 and: [hasTen]]
then: ['vend ten'];
default: ['raise out of cash signal']! !
Andreas Raab wrote:
> Phil,
>
> > This best implemenation of the case statement is given in the article,
> > 'Smalltalk Case Statements' by Paul Baumann in the July/August 1997 issue
> > of The Smalltalk Report. Baumann's article illustrates the ease of
> > providing support for a case statement, gives examples of their use, and
> > explains why the example case statement is appropiate.
>
> I haven't seen the article. Could you briefly summarize what it's
> all about?!
>
> Thanks,
> Andreas
> --
> Linear algebra is your friend - Trigonometry is your enemy.
> +===== Andreas Raab ============= (raab at isg.cs.uni-magdeburg.de) =====+
> I Department of Simulation and Graphics Phone: +49 391 671 8065 I
> I University of Magdeburg, Germany Fax: +49 391 671 1164 I
> +=============< http://isgwww.cs.uni-magdeburg.de/~raab >=============+
More information about the Squeak-dev
mailing list
|