-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy.st
More file actions
176 lines (146 loc) · 3.81 KB
/
my.st
File metadata and controls
176 lines (146 loc) · 3.81 KB
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
"-------------------------------------------------------"
! Object methods !
s
"Return self as a string."
^self printString !
oo
"Show self on Transcript"
Transcript nextPutAll: self s;
cr!!
! Symbol methods !
talk
"Shorthand for reading in code"
^FileStream fileIn: self asString,'.st' !
"-------------------------------------------------------"
! String methods !
talk
"Shorthand for reading in code"
^FileStream fileIn: self,'.st' !
asWords
"Returns self, divided on whitespace."
^(self tokenize: '\s+') asArray !
right: n
"Returns self, padded right with spaces."
^self right: n with: $ !
right: n with: char
"Returns self, padded right with character char."
|out|
out := WriteStream on:''.
1 to: n - self size do: [:i| out nextPut: char ].
out nextPutAll: self.
^out contents !
left: n
"Returns self, padded left with spaces."
^self left: n with: $ !
left: n with: char
"Returns self, padded left with character char."
|out s|
out := WriteStream on: ''.
out nextPutAll: self.
1 to: n - self size do: [:i| out nextPut: char ].
^out contents !
!
! String class methodsFor: 'testing' !
goodAsWords
^'i want candy' asWords = #('i' 'want' 'candy') !
goodLeft
^('123' left: 10) = '123 ' !
goodRight
^('123' right: 10) = ' 123' !
!
"-------------------------------------------------------"
! Class methods !
public
"Auto-create get and set methods for all instance vars."
|getter setter|
self instanceVariableString asWords do: [:what |
getter := '%1 [
"Answer the receiver''s %1."
^%1 ]' % {what} .
setter := '%1: aValue [
"Set the receiver''s %1."
%1 := aValue ]' % {what} .
self compile: getter;
compile: setter. ] !
!
"-------------------------------------------------------"
! Class methods !
sub: kid
"Shorthand for sub-classing."
^self sub: kid has: '' shares: '' !
sub: kid has: vars
"Shorthand for sub-classing."
^self sub: kid has: vars shares: ''!
sub: kid has: vars1 shares: vars2
"Shorthand for sub-classing."
^(self
subclass: kid
instanceVariableNames: vars1
classVariableNames: vars2
poolDictionaries: ''
category: nil)
public !
!
"-------------------------------------------------------"
"All Magic methods always run `init` on new instances
(so subclasses of Magic that implement `init` get it
called automatically)."
Object sub: #Magic!
! Magic class methods !
new
^super new init !!
! Magic methods !
init !!
! Magic methods !
visit: aBlock
"To heck with encapulation. Walk over the instance vars."
| num |
num := self class instSize + self basicSize.
1 to: num do: [:i |
(self instVarAt: i) visit: aBlock ]
!!
Magic sub: #Employee has: 'name age shoesize'
! Employee methods !
init
self name: 'freda';
age: 21;
shoesize: 0 !
printOn: aStream
aStream
nextPutAll: 'Emp(';
nextPutAll: ':name ',name s;
nextPutAll: ' :age ',age s;
nextPutAll: ' :shoesize ',shoesize s;
nextPutAll: ')' !
!
!Collection class methodsFor: 'testing' !
goodB4Now
#(10 21 32 43 54) b4Now: [:b4 :now|
((now-b4)/b4) asFloat oo] !!
! Object class methodsFor: 'testing' !
goodVisit
|x y z w|
x := (Employee new) name: 'tammy'.
y := (Employee new) name: 'tammy'.
z := (Employee new) name: 'Huy'; age: 18.
w := {1. 2. #abc. z. {x. x. x. {y.}.}. 4. {{{5.}.}.}.}.
"z visit: [:n| n oo].
"
w visit:[:a| a oo]
!!
"--------------STUDENT CODE--------------"
! Collection methods !
b4Now: aBlock
1 to: (self size - 1) do: [ :x | aBlock value: (self at: x) value: (self at: (x + 1)). ].
!
visit: aBlock
self do: [:x | x visit: aBlock]
!!
! String methods !
visit: aBlock
aBlock value: self.
!!
! Object methods !
visit: aBlock
aBlock value: self.
!!