initial checkin
authorClaus Gittinger <cg@exept.de>
Tue, 19 Nov 2019 11:19:57 +0100
changeset 5256 ec058e3c82a1
parent 5255 aed43d6cf187
child 5257 25c9816018d2
initial checkin
IteratorStream.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IteratorStream.st	Tue Nov 19 11:19:57 2019 +0100
@@ -0,0 +1,127 @@
+"
+ COPYRIGHT (c) 2019 by Claus Gittinger
+              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 }"
+
+PeekableStream subclass:#IteratorStream
+	instanceVariableNames:'idx iterator contentsSpecies'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Streams'
+!
+
+!IteratorStream class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 2019 by Claus Gittinger
+              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
+"
+    This is a pseudo stream, which generates values by using an iterator action;
+    this is a block which is called to generate the value to read.
+    The block gets the index (1..) as argument.
+
+    [author:]
+        Claus Gittinger
+
+    [instance variables:]
+
+    [class variables:]
+
+    [see also:]
+        Iterator
+"
+!
+
+example
+"
+        |s|
+
+        s := IteratorStream on:[:i | i].
+        s next:10.
+        s next:10.
+
+        s := IteratorStream on:[:i | i*2].
+        s next:10.
+
+"
+! !
+
+!IteratorStream class methodsFor:'instance creation'!
+
+on:aBlock
+    ^ self basicNew initialize iterator:aBlock
+! !
+
+!IteratorStream methodsFor:'accessing'!
+
+contentsSpecies
+    "return a class of which instances will be returned, when
+     parts of the collection are asked for. 
+     (see upTo-kind of methods in Stream)"
+
+    ^ contentsSpecies ? Array
+!
+
+contentsSpecies:aCollectionClass
+    contentsSpecies := aCollectionClass.
+!
+
+iterator:aOneArgBlock
+    iterator := aOneArgBlock.
+!
+
+position
+    ^ idx
+!
+
+position:newPosition
+    idx := newPosition
+! !
+
+!IteratorStream methodsFor:'initialization'!
+
+initialize
+    super initialize.
+    idx := 0.
+! !
+
+!IteratorStream methodsFor:'reading'!
+
+next
+    "return the next element from the stream by evaluating the nextBlock"
+
+    iterator notNil ifTrue:[
+        idx := idx + 1.
+        ^ iterator value:idx
+    ].
+    self error:'iterator is undefined'
+! !
+
+!IteratorStream class methodsFor:'documentation'!
+
+version_CVS
+    ^ '$Header$'
+! !
+