RecursiveSeriesStream.st
changeset 5257 25c9816018d2
child 5259 fa49d09cad7b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RecursiveSeriesStream.st	Tue Nov 19 11:20:14 2019 +0100
@@ -0,0 +1,104 @@
+"{ Package: 'stx:libbasic2' }"
+
+"{ NameSpace: Smalltalk }"
+
+IteratorStream subclass:#RecursiveSeriesStream
+	instanceVariableNames:'lastValue'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Streams'
+!
+
+!RecursiveSeriesStream class methodsFor:'documentation'!
+
+documentation
+"
+    documentation to be added.
+
+    class:
+        <a short class summary here, describing what instances represent>
+
+    responsibilities:    
+        <describing what my main role is>
+
+    collaborators:    
+        <describing with whom and how I talk to>
+
+    API:
+        <public api and main messages>
+        
+    example:
+        <a one-line examples on how to use - can also be in a separate example method>
+
+    implementation:
+        <implementation points>
+
+    [author:]
+        cg
+
+    [instance variables:]
+
+    [class variables:]
+
+    [see also:]
+
+"
+!
+
+example
+"
+        |s|
+
+        s := RecursiveSeriesStream on:[:n | n/2] startValue:1.
+        s next:10.
+        s next:10.
+
+        s := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:10.
+        s next:10.
+        s next:10.
+"
+!
+
+example2
+"
+        |s|
+
+        1 to:100 collect:[:n0 |
+                s := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:n0.
+                s next:10.   
+        ]
+"
+! !
+
+!RecursiveSeriesStream class methodsFor:'instance creation'!
+
+on:aBlock startValue:startValue
+    ^ self basicNew initialize iterator:aBlock startValue:startValue
+! !
+
+!RecursiveSeriesStream methodsFor:'accessing'!
+
+iterator:aOneArgBlock startValue:startValue
+    iterator := aOneArgBlock.
+    lastValue := startValue.
+! !
+
+!RecursiveSeriesStream methodsFor:'reading'!
+
+next
+    "return the next element from the stream by evaluating the nextBlock"
+
+    |prevValue|
+
+    prevValue := lastValue.
+    lastValue := iterator value:lastValue.
+    ^ prevValue
+
+! !
+
+!RecursiveSeriesStream class methodsFor:'documentation'!
+
+version_CVS
+    ^ '$Header$'
+! !
+