ConsStream.st
changeset 1378 c588900169e4
child 1482 cea67ec35cc2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConsStream.st	Tue Dec 09 15:26:19 2003 +0100
@@ -0,0 +1,110 @@
+"
+ 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' }"
+
+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
+
+    [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 
+!
+
+next
+    |rslt|
+
+    list isNil ifTrue:[
+        ^ self pastEnd
+    ].
+    rslt := list car.
+    list := list cdr.
+    ^ rslt
+! !
+
+!ConsStream class methodsFor:'documentation'!
+
+version
+    ^ '$Header: /cvs/stx/stx/libbasic2/ConsStream.st,v 1.1 2003-12-09 14:26:19 cg Exp $'
+! !