-
Notifications
You must be signed in to change notification settings - Fork 2
/
hacks.st
163 lines (141 loc) · 3.84 KB
/
hacks.st
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
Object extend [
namespaceChain [
<category: '*shampoo-hacks'>
| ns chain |
ns := self environment.
chain := OrderedCollection new.
[ns ~= Smalltalk] whileTrue:
[chain add: ns. ns := ns environment].
^chain reverse
]
]
Collection extend [
elementsString [
<category: '*shampoo-hacks'>
^String join: self separatedBy: ' '
]
asStringArray [
<category: '*shampoo-hacks'>
"Many reflection methods return different results in various dialects.
#instVarNames in GNU Smalltalk returns an IdentitySet of Symbols, the
same method returns an Array of Strings in Squeak.
This kludge works as an abstraction over it all"
^(self collect: [:each | each asString]) asArray
]
or [
<category: '*shampoo-hacks'>
self isEmpty ifTrue: [^false].
^self fold: [:a :b | a | b]
]
and [
<category: '*shampoo-hacks'>
self isEmpty ifTrue: [^true].
^self fold: [:a :b | a & b]
]
]
BlockClosure extend [
not [
<category: '*shampoo-hacks'>
self numArgs ~= 1 ifTrue:
[^self error: '#not is for single argument blocks only'].
^[:value | (self value: value) not]
]
]
PositionableStream extend [
forwardWhile: aBlock [
<category: '*shampoo-hacks'>
[self atEnd not and: [aBlock value: self peek]]
whileTrue: [self next]
]
]
SequenceableCollection extend [
drop: anInteger [
<category: '*shampoo-hacks'>
anInteger > self size ifTrue: [^self class new].
^self copyFrom: anInteger + 1
]
take: anInteger [
<category: '*shampoo-hacks'>
anInteger > self size ifTrue: [^self copy].
anInteger = 0 ifTrue: [^self class new].
^self copyFrom: 1 to: anInteger
]
breakIf: aBlock [
<category: '*shampoo-hacks'>
| r |
r := ReadStream on: self.
r forwardWhile: aBlock not.
^{ self copyFrom: 1 to: r position.
r upToEnd }
]
dropWhile: aBlock [
<category: '*shampoo-hacks'>
| r |
r := ReadStream on: self.
r forwardWhile: aBlock.
^self copyFrom: r position + 1
]
takeWhile: aBlock [
<category: '*shampoo-hacks'>
| r |
r := ReadStream on: self.
r forwardWhile: aBlock.
^self copyFrom: 1 to: r position
]
]
String class extend [
crlf [
<category: '*shampoo-hacks'>
^String new writeStream
nextPut: Character cr;
nextPut: Character lf;
contents
]
]
String extend [
isClosingBracket [
<category: '*shampoo-hacks'>
^self trimSeparators = ']'
]
]
PackageLoader class extend [
reloadPackage: aPackageName [
<category: '*shampoo-hacks'>
| base |
base := PackageLoader directoryFor: aPackageName.
(PackageLoader fileInsFor: aPackageName) do:
[:e | FileStream fileIn: (base / e) file name].
]
]
Namespace current: Sockets [
StreamSocket extend [
crlf [
<category: '*shampoo-hacks'>
self nextPutAll: String crlf
]
]
]
Object subclass: Decorator [
| underlyingObject |
Decorator class >> on: anObject [
<category: 'instance creation'>
^self new
underlyingObject: anObject;
yourself
]
underlyingObject: anObject [
<category: 'accessors'>
underlyingObject := anObject
]
underlyingObject [
<category: 'accessors'>
^underlyingObject
]
doesNotUnderstand: aMessage [
<category: 'decorating'>
"Proxy an unknown message to an underlying object"
^underlyingObject
perform: aMessage selector
withArguments: aMessage arguments
]
]