CollectingReadStream.st
author Stefan Vogel <sv@exept.de>
Sat, 10 Aug 2013 13:31:17 +0200
changeset 3083 1befd13e063c
parent 2673 ea71dab56953
child 3092 98fe1a07634d
permissions -rw-r--r--
class: CollectingReadStream changed: #position0Based #position1Based use #position/#position: instead of #positionXBased

"{ Package: 'stx:libbasic2' }"

PeekableStream subclass:#CollectingReadStream
	instanceVariableNames:'collectBlock inStream'
	classVariableNames:''
	poolDictionaries:''
	category:'Streams-Misc'
!

!CollectingReadStream class methodsFor:'documentation'!

documentation
"
    A stream which provides the result of applying a collectBlock
    to the elements of an underlying stream. 
"
!

examples
"
                                                            [exBegin]
    |s|

    s := CollectingReadStream 
            on:#(1 2 3 4 5 6 7 8) readStream
            collecting:[:each | each squared].
    s upToEnd  
                                                            [exEnd]

                                                            [exBegin]
    |s|

    s := (#(1 2 3 4 5 6 7 8) readStream 
            selecting:[:n | n odd])
                collecting:[:n | n squared].
    s upToEnd    
                                                            [exEnd]
"
! !

!CollectingReadStream class methodsFor:'instance creation'!

on:aStream collecting:aBlock
    ^ self basicNew on:aStream collecting:aBlock
! !

!CollectingReadStream methodsFor:'instance creation'!

on:aStream collecting:aBlock
    inStream := aStream.
    collectBlock := aBlock.
! !

!CollectingReadStream methodsFor:'queries'!

atEnd
    ^ inStream atEnd
! !

!CollectingReadStream methodsFor:'stream protocol'!

next
    inStream atEnd ifTrue:[
        ^ self pastEndRead
    ].
    ^ collectBlock value:(inStream next)
!

nextOrNil
    |ch|

    ch := inStream nextOrNil.
    ch notNil ifTrue:[
        collectBlock value:ch
    ].
    ^ ch

    "Created: / 26-10-2011 / 17:29:01 / cg"
!

nextPeek
    self next.
    ^ self peek

    "Created: / 26-10-2011 / 17:25:16 / cg"
!

peek
    ^ inStream peek

    "Created: / 26-10-2011 / 17:24:59 / cg"
!

peekOrNil
    ^ inStream peekOrNil

    "Created: / 26-10-2011 / 17:25:32 / cg"
!

position
    ^ inStream position

    "Created: / 26-10-2011 / 17:25:08 / cg"
!

position0Based
    <resource: #obsolete>
    ^ inStream position

    "Created: / 26-10-2011 / 17:23:56 / cg"
!

position1Based
    <resource: #obsolete>
    ^ inStream position + 1

    "Created: / 26-10-2011 / 17:22:42 / cg"
!

readStream
    ^ self

    "Created: / 26-10-2011 / 17:25:43 / cg"
! !

!CollectingReadStream class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/CollectingReadStream.st,v 1.4 2013-08-10 11:31:17 stefan Exp $'
! !