--- a/FilteringStream.st Mon Dec 07 15:54:21 2009 +0100
+++ b/FilteringStream.st Fri Dec 11 10:23:51 2009 +0100
@@ -9,7 +9,7 @@
other person. No title to or ownership of the software is
hereby transferred.
"
-"{ Package: 'stx:goodies' }"
+"{ Package: 'stx:libbasic2' }"
PeekableStream subclass:#FilteringStream
instanceVariableNames:'inputStream outputStream filter readAhead'
@@ -51,7 +51,7 @@
and element, process it and return it.
Or, in pushMode, by having someone else writing elements via
- nextPut:; it will then process the element, and send it to its
+ nextPut:; it will then process the element, and send it to its
output stream.
Mixing modes does not make sense, since if pulled, data will not
@@ -61,19 +61,20 @@
responds to the basic Stream protocol can be connected
(a Transcript, a RandomNumber generator or even a Plug will do as well).
+ Similar, but not quite the same as SelectingReadStream / CollectingReadStream.
+
[instance variables:]
- inputStream <Stream> the stream from which elements are read
- outputStream <Stream> the stream to which elements are written
- filter <Block> the filter block;
- unbound <Boolean> if true, the stream is unbound.
+ inputStream <Stream> the stream from which elements are read
+ outputStream <Stream> the stream to which elements are written
+ filter <Block> the filter block;
+ unbound <Boolean> if true, the stream is unbound.
[author:]
- Claus Gittinger
+ Claus Gittinger
[See also:]
- ReadStream WriteStream
+ ReadStream WriteStream
"
-
!
examples
@@ -86,19 +87,19 @@
[exBegin]
|in pusher|
- in := 'Makefile' asFilename readStream.
+ in := 'Make.proto' asFilename readStream.
pusher := FilteringStream readingFrom:in writingTo:Transcript.
pusher filterUpToEnd
[exEnd]
- filter random numbers:
+ filter random numbers in [0.5 .. 0.6]:
[exBegin]
|in filter|
in := Random new.
- filter := FilteringLineStream readingFrom:in.
+ filter := FilteringStream readingFrom:in.
filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].
20 timesRepeat:[
@@ -106,7 +107,8 @@
]
[exEnd]
- filtering prime numbers:
+ filtering prime numbers (that's just a demo - not really useful, because we will end
+ in a recursion error if many filters are stacked):
[exBegin]
|num generator primeFilter addFilter|
@@ -132,7 +134,7 @@
addFilter value:2.
primeFilter inputStream:generator.
- 10000 timesRepeat:[
+ 1000 timesRepeat:[
|nextPrime|
nextPrime := primeFilter next.
@@ -141,9 +143,6 @@
]
[exEnd]
"
-
-
-
! !
!FilteringStream class methodsFor:'instance creation'!
@@ -260,24 +259,24 @@
|input output|
readAhead notNil ifTrue:[
- ^ readAhead
+ ^ readAhead
].
[inputStream atEnd] whileFalse:[
- "/ get an element
- input := inputStream peek.
- filter isNil ifTrue:[
- ^ input
- ].
+ "/ get an element
+ input := inputStream peek.
+ filter isNil ifTrue:[
+ ^ input
+ ].
- "/ filter it - this may return nil, to eat it
- inputStream next.
- output := filter value:input.
- output notNil ifTrue:[
- "/ good - output it
- readAhead := output.
- ^ output.
- ].
+ "/ filter it - this may return nil, to eat it
+ inputStream next.
+ output := filter value:input.
+ output notNil ifTrue:[
+ "/ good - output it
+ readAhead := output.
+ ^ output.
+ ].
].
^ self pastEndRead
@@ -289,24 +288,24 @@
|input output|
readAhead notNil ifTrue:[
- ^ readAhead
+ ^ readAhead
].
[inputStream atEnd] whileFalse:[
- "/ get an element
- input := inputStream peek.
- filter isNil ifTrue:[
- ^ input
- ].
+ "/ get an element
+ input := inputStream peek.
+ filter isNil ifTrue:[
+ ^ input
+ ].
- "/ filter it - this may return nil, to eat it
- inputStream next.
- output := filter value:input.
- output notNil ifTrue:[
- "/ good - output it
- readAhead := output.
- ^ output.
- ].
+ "/ filter it - this may return nil, to eat it
+ inputStream next.
+ output := filter value:input.
+ output notNil ifTrue:[
+ "/ good - output it
+ readAhead := output.
+ ^ output.
+ ].
].
^ nil
! !
@@ -409,17 +408,17 @@
readAhead notNil ifTrue:[^ false].
filter isNil ifTrue:[
- "/ then, its easy
- ^ inputStream atEnd
+ "/ then, its easy
+ ^ inputStream atEnd
].
- "/ with a filter, things are more complicated,
+ "/ with a filter, things are more complicated,
"/ since we cannot tell, without asking the filter ...
[inputStream atEnd] whileFalse:[
- nextElement := inputStream next.
- readAhead := filter value:nextElement.
- readAhead notNil ifTrue:[^ false].
+ nextElement := inputStream next.
+ readAhead := filter value:nextElement.
+ readAhead notNil ifTrue:[^ false].
].
^ true
@@ -443,7 +442,7 @@
rawPosition := inputStream position.
readAhead notNil ifTrue:[
- rawPosition := rawPosition - 1
+ rawPosition := rawPosition - 1
].
^ rawPosition
!
@@ -455,7 +454,7 @@
rawPosition := inputStream position0Based.
readAhead notNil ifTrue:[
- rawPosition := rawPosition - 1
+ rawPosition := rawPosition - 1
].
^ rawPosition
!
@@ -467,7 +466,7 @@
rawPosition := inputStream position1Based.
readAhead notNil ifTrue:[
- rawPosition := rawPosition - 1
+ rawPosition := rawPosition - 1
].
^ rawPosition
! !
@@ -475,5 +474,9 @@
!FilteringStream class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.12 2009-01-11 15:39:48 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.13 2009-12-11 09:23:51 cg Exp $'
+!
+
+version_CVS
+ ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.13 2009-12-11 09:23:51 cg Exp $'
! !