ConsStream.st
author convert-repo
Fri, 20 Dec 2019 04:27:52 +0000
changeset 5399 6b0583308f5d
parent 5249 08af43aaea4c
permissions -rw-r--r--
update tags

"
 COPYRIGHT (c) 2003 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

Stream subclass:#ConsStream
	instanceVariableNames:'list'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Linked'
!

!ConsStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2003 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    A stream on a list (of conses)
    Conses are not heavily used by Smalltalk (actually: not at all).
    Consider this a demo class.

    [author:]
        Claus Gittinger (Dec 2003)

    [see also:]

"
!

examples
"
                                                                            [exBegin]
    |list s|

    list := Cons fromArray:#(1 2 3 4).   
    s := list readStream.
    [s atEnd] whileFalse:[
        Transcript showCR:(s next).
    ].
                                                                            [exEnd]
                                                                            [exBegin]
    |gen allNumbers s|

    gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
    allNumbers := gen value:1.

    s := allNumbers readStream.
    100 timesRepeat:[
        Transcript showCR:(s next).
    ].
                                                                            [exEnd]
"
! !

!ConsStream class methodsFor:'instance creation'!

on:aCons
    ^ self basicNew list:aCons
! !

!ConsStream methodsFor:'accessing'!

list:something
    list := something.
! !

!ConsStream methodsFor:'reading'!

atEnd
    ^ list isNil 
! !

!ConsStream methodsFor:'stream protocol-reading'!

next
    |rslt|

    list isNil ifTrue:[
        ^ self pastEndRead
    ].
    rslt := list car.
    list := list cdr.
    ^ rslt
! !

!ConsStream class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !