RecursiveSeriesStream.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5262 03f0b6c2ca34
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:

"{ Encoding: utf8 }"

"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

IteratorStream subclass:#RecursiveSeriesStream
	instanceVariableNames:'lastValue endValue'
	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
"
        |collatz|

        collatz := [:n | RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:n].
        (collatz[20]) next:20.  
        (collatz[24]) next:20.  
        (collatz[25]) next:30.   
"
!

example2
"
        |max bits collatz newNums|

        newNums := OrderedCollection new.        
        bits := SortedCollection new.
        max := 2.
        collatz := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[(n*3+1)/2]] startValue:1.
        1 to:100000 do:[:n0 |
            newNums removeAll.    
            collatz startValue:n0.
            [
                |n|
                n := collatz next.
                newNums add:n.
                n <= max
            ] whileFalse.
            bits addAll:newNums.
            [bits notEmpty and:[bits first <= (max+1)]] whileTrue:[max := max max:(bits first). bits removeFirst].
        ].
        self halt.
"
! !

!RecursiveSeriesStream class methodsFor:'instance creation'!

on:aBlock startValue:startValue
    ^ self basicNew initialize iterator:aBlock startValue:startValue
! !

!RecursiveSeriesStream methodsFor:'accessing'!

atEnd
    ^ lastValue = endValue
!

endValue:something
    "define a stop value"

    endValue := something.
!

iterator:aOneArgBlock startValue:startValue
    iterator := aOneArgBlock.
    lastValue := startValue.
!

startValue:startValue
    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$'
! !