401
|
1 |
Stream subclass:#FilteringStream
|
|
2 |
instanceVariableNames:'inputStream outputStream filter unbound'
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Streams'
|
|
6 |
!
|
|
7 |
|
|
8 |
!FilteringStream class methodsFor:'documentation'!
|
|
9 |
|
|
10 |
examples
|
|
11 |
"
|
|
12 |
filter random numbers:
|
|
13 |
|
|
14 |
|in filter|
|
|
15 |
|
|
16 |
in := Random new.
|
|
17 |
|
|
18 |
filter := FilteringLineStream basicNew.
|
|
19 |
filter inputStream:in.
|
|
20 |
filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].
|
|
21 |
|
|
22 |
20 timesRepeat:[
|
|
23 |
Transcript showCR:(filter next printString).
|
|
24 |
]
|
|
25 |
|
|
26 |
filtering prime numbers:
|
|
27 |
|
|
28 |
|num generator primeFilter addFilter|
|
|
29 |
|
|
30 |
num := 1.
|
|
31 |
generator := Plug new.
|
|
32 |
generator respondTo:#next
|
|
33 |
with:[num := num + 1. num].
|
|
34 |
generator respondTo:#atEnd
|
|
35 |
with:[false].
|
|
36 |
|
|
37 |
addFilter := [:prime | |newFilter|
|
|
38 |
newFilter := FilteringLineStream basicNew.
|
|
39 |
newFilter unbound:true.
|
|
40 |
newFilter filter:[:num | (num \\ prime) == 0 ifTrue:[
|
|
41 |
nil
|
|
42 |
] ifFalse:[
|
|
43 |
num
|
|
44 |
]
|
|
45 |
].
|
|
46 |
newFilter inputStream:primeFilter.
|
|
47 |
primeFilter := newFilter
|
|
48 |
].
|
|
49 |
|
|
50 |
addFilter value:2.
|
|
51 |
primeFilter inputStream:generator.
|
|
52 |
|
|
53 |
100000 timesRepeat:[
|
|
54 |
|nextPrime|
|
|
55 |
|
|
56 |
nextPrime := primeFilter next.
|
|
57 |
addFilter value:nextPrime.
|
|
58 |
Transcript showCR:nextPrime.
|
|
59 |
]
|
|
60 |
"
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
! !
|
|
65 |
|
|
66 |
!FilteringStream methodsFor:'access - pull-reading'!
|
|
67 |
|
|
68 |
filterUpToEnd
|
|
69 |
"pull input from inputStream up to the end,
|
|
70 |
push it filtered into the outputStream"
|
|
71 |
|
|
72 |
[inputStream atEnd] whileFalse:[
|
|
73 |
self nextPut:(inputStream next)
|
|
74 |
].
|
|
75 |
|
|
76 |
"Modified: 2.7.1996 / 21:02:49 / cg"
|
|
77 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
78 |
!
|
|
79 |
|
|
80 |
next
|
|
81 |
"pull input from inputStream and
|
|
82 |
push it filtered into the outputStream"
|
|
83 |
|
|
84 |
|input output|
|
|
85 |
|
|
86 |
[inputStream atEnd] whileFalse:[
|
|
87 |
input := inputStream next.
|
|
88 |
output := filter value:input.
|
|
89 |
output notNil ifTrue:[
|
|
90 |
^ output.
|
|
91 |
].
|
|
92 |
].
|
|
93 |
^ nil
|
|
94 |
|
|
95 |
"Created: 2.7.1996 / 21:09:58 / cg"
|
|
96 |
! !
|
|
97 |
|
|
98 |
!FilteringStream methodsFor:'access - push-writing'!
|
|
99 |
|
|
100 |
nextPut:something
|
|
101 |
"push something through the filter"
|
|
102 |
|
|
103 |
|output|
|
|
104 |
|
|
105 |
output := filter value:something.
|
|
106 |
output notNil ifTrue:[
|
|
107 |
outputStream nextPut:output
|
|
108 |
]
|
|
109 |
|
|
110 |
"Created: 2.7.1996 / 21:07:58 / cg"
|
|
111 |
! !
|
|
112 |
|
|
113 |
!FilteringStream methodsFor:'accessing'!
|
|
114 |
|
|
115 |
filter
|
|
116 |
"return the filter"
|
|
117 |
|
|
118 |
^ filter
|
|
119 |
|
|
120 |
"Modified: 2.7.1996 / 21:03:36 / cg"
|
|
121 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
122 |
!
|
|
123 |
|
|
124 |
filter:something
|
|
125 |
"set the filter"
|
|
126 |
|
|
127 |
filter := something.
|
|
128 |
|
|
129 |
"Modified: 2.7.1996 / 21:03:40 / cg"
|
|
130 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
131 |
!
|
|
132 |
|
|
133 |
inputStream
|
|
134 |
"return the inputStream"
|
|
135 |
|
|
136 |
^ inputStream
|
|
137 |
|
|
138 |
"Modified: 2.7.1996 / 21:03:43 / cg"
|
|
139 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
140 |
!
|
|
141 |
|
|
142 |
inputStream:something
|
|
143 |
"set the inputStream"
|
|
144 |
|
|
145 |
inputStream := something.
|
|
146 |
|
|
147 |
"Modified: 2.7.1996 / 21:03:46 / cg"
|
|
148 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
149 |
!
|
|
150 |
|
|
151 |
outputStream
|
|
152 |
"return the outputStream"
|
|
153 |
|
|
154 |
^ outputStream
|
|
155 |
|
|
156 |
"Modified: 2.7.1996 / 21:03:49 / cg"
|
|
157 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
158 |
!
|
|
159 |
|
|
160 |
outputStream:something
|
|
161 |
"set the outputStream"
|
|
162 |
|
|
163 |
outputStream := something.
|
|
164 |
|
|
165 |
"Modified: 2.7.1996 / 21:03:52 / cg"
|
|
166 |
"Created: 2.7.1996 / 21:06:42 / cg"
|
|
167 |
!
|
|
168 |
|
|
169 |
unbound
|
|
170 |
"return unbound"
|
|
171 |
|
|
172 |
^ unbound
|
|
173 |
|
|
174 |
"Created: 2.7.1996 / 21:25:40 / cg"
|
|
175 |
!
|
|
176 |
|
|
177 |
unbound:something
|
|
178 |
"set unbound"
|
|
179 |
|
|
180 |
unbound := something.
|
|
181 |
|
|
182 |
"Created: 2.7.1996 / 21:25:40 / cg"
|
|
183 |
! !
|
|
184 |
|
|
185 |
!FilteringStream methodsFor:'queries'!
|
|
186 |
|
|
187 |
atEnd
|
|
188 |
unbound ifTrue:[^ false].
|
|
189 |
^ inputStream atEnd
|
|
190 |
|
|
191 |
"Created: 2.7.1996 / 21:19:51 / cg"
|
|
192 |
"Modified: 2.7.1996 / 21:25:53 / cg"
|
|
193 |
! !
|
|
194 |
|
|
195 |
!FilteringStream class methodsFor:'documentation'!
|
|
196 |
|
|
197 |
version
|
|
198 |
^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.1 1996-07-02 19:28:43 cg Exp $'
|
|
199 |
! !
|