File: Case.st

package info (click to toggle)
gnu-smalltalk 3.2.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,688 kB
  • ctags: 14,104
  • sloc: ansic: 87,424; sh: 22,729; asm: 8,465; perl: 4,513; cpp: 3,548; xml: 1,669; awk: 1,581; yacc: 1,357; makefile: 1,237; lisp: 855; lex: 843; sed: 258; objc: 124
file content (229 lines) | stat: -rw-r--r-- 5,411 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"======================================================================
|
|   Case syntax for Smalltalk
|
|
 ======================================================================"


"======================================================================
|
| Written by Ulf Dambacher.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
| 
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
| details.
| 
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
|
 ======================================================================"

Object subclass: #Case
	  instanceVariableNames: 'test found result'
	  classVariableNames: ''
	  poolDictionaries: ''
	  category: 'Examples-Useful tools'
!

Case comment:
'A nice class for switch-like tests. Slower than cascaded ifs but clearer.
#case:do: uses identity for testing (useful since you''ll likely use Case
with symbols, integers, characters, and the like), #ifEqualTo:do: uses
equality.

e.g.

Case new test: myObject;
    case: 5				   do: [ ''got five''	    printNl ];
    when: [ :testVal | testVal isInteger ] do: [ ''an integer''     printNl ];
    else: 				       [ :testVal | testVal printNl ]

You can use (Case test: myObject) instead of the first line above. Which of
the two possibilities is clearer, it is a matter of taste.'!

!Case class methodsFor: 'instance creation'!

test: anObject
    ^self new test: anObject
! !

!Case methodsFor: 'testing'!

test: anObject
    test := anObject.
    found := false.
!

reset
    found := false
!

else: aBlock
    ^found
	ifFalse: [ self do: aBlock ]
	ifTrue: [ result ].
!

case: anObject do: aBlock
    ^(found not and: [test == anObject])
	ifTrue: [ self do: aBlock ]
	ifFalse: [ result ].
!

ifEqualTo: anObject do: aBlock
    ^(found not and: [test = anObject])
	ifTrue: [ self do: aBlock ]
	ifFalse: [ result ].
!

when: aBlock do: aBlock2
    ^(found not and: [aBlock value: test])
	ifTrue: [ self do: aBlock ]
	ifFalse: [ result ].
! !

!Case methodsFor: 'private'!

do: aBlock
    found := true.
    ^result := (aBlock cull: test)
! !


Object subclass: #Switch
	  instanceVariableNames: 'values defaultBlock'
	  classVariableNames: ''
	  poolDictionaries: ''
	  category: 'Examples-Useful tools'
!

Object subclass: #SwitchCase
	  instanceVariableNames: 'secondParameter evaluationBlock actionBlock result'
	  classVariableNames: ''
	  poolDictionaries: ''
	  category: 'Examples-Useful tools'
!

Switch comment:
'Another class for switch-like tests.  This is reusable, i.e. an instance
stores its case-do pairs.

e.g.

(Switch new)
    case: "value or Block" do: [...];
    ...
    default: [...];
    on: value.         "#= equality matching of cases"

(Switch new)
    case: "value or Block" do: [...];
    ...
    default: [...];
    exactlyOn: value.  "#== identity matching of cases"
'!

!Switch class methodsFor: 'instance creation'!

new
    ^self basicNew values: (ReadWriteStream on: Array new: 5)
! !

!Switch methodsFor: 'initialization'!

values: aStream
    values := aStream
!

default: aBlock
    defaultBlock := aBlock
!

case: anObjectOrBlock do: aBlock
    | block |
    block := anObjectOrBlock class == BlockClosure
	ifTrue: [
	    [ :object :block :identity | block value: object ]
	ifFalse: [
	    [ :object :comparison :identity |
		identity
		    ifTrue: [ object == comparison ]
		    ifFalse: [ object = comparison ]
	    ]
    ].
    values nextPut: (SwitchCase new
	secondParameter: anObjectOrBlock
	evaluationBlock: block
	actionBlock: aBlock)
! !

!Switch methodsFor: 'evaluation'!

on: anObject
    | done case |
    done := false.
    values reset.
    [ values atEnd ] whileFalse: [
	case := values next.
	(case evaluateIfEqualTo: anObject) ifTrue: [ ^case result ].
    ].
    ^defaultBlock value! !

exactlyOn: anObject
    | done case |
    done := false.
    values reset.
    [ values atEnd ] whileFalse: [
	case := values next.
	(case evaluateIfIdenticalTo: anObject) ifTrue: [ ^case result ].
    ].
    ^defaultBlock value! !

identityOn: anObject
    | done case |
    done := false.
    values reset.
    [ values atEnd ] whileFalse: [
	case := values next.
	(case evaluateIfIdenticalTo: anObject) ifTrue: [ ^case result ].
    ].
    ^defaultBlock value! !

!SwitchCase methodsFor: 'evaluation'!

evaluateIfEqualTo: object
    ^(evaluationBlock value: object value: secondParameter value: false)
	ifTrue: [ result := actionBlock value ];
	yourself
!

evaluateIfIdenticalTo: object
    ^(evaluationBlock value: object value: secondParameter value: true)
	ifTrue: [ result := actionBlock value ];
	yourself
!

result
    | answer |
    answer := result.
    result := nil.
    ^answer
! !

!SwitchCase methodsFor: 'initialization'!

secondParameter: sP evaluationBlock: eB actionBlock: aB
    secondParameter := sP.
    evaluationBlock := eB.
    actionBlock := aB
! !