5257
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
|
5 |
IteratorStream subclass:#RecursiveSeriesStream
|
|
6 |
instanceVariableNames:'lastValue'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'Streams'
|
|
10 |
!
|
|
11 |
|
|
12 |
!RecursiveSeriesStream class methodsFor:'documentation'!
|
|
13 |
|
|
14 |
documentation
|
|
15 |
"
|
|
16 |
documentation to be added.
|
|
17 |
|
|
18 |
class:
|
|
19 |
<a short class summary here, describing what instances represent>
|
|
20 |
|
|
21 |
responsibilities:
|
|
22 |
<describing what my main role is>
|
|
23 |
|
|
24 |
collaborators:
|
|
25 |
<describing with whom and how I talk to>
|
|
26 |
|
|
27 |
API:
|
|
28 |
<public api and main messages>
|
|
29 |
|
|
30 |
example:
|
|
31 |
<a one-line examples on how to use - can also be in a separate example method>
|
|
32 |
|
|
33 |
implementation:
|
|
34 |
<implementation points>
|
|
35 |
|
|
36 |
[author:]
|
|
37 |
cg
|
|
38 |
|
|
39 |
[instance variables:]
|
|
40 |
|
|
41 |
[class variables:]
|
|
42 |
|
|
43 |
[see also:]
|
|
44 |
|
|
45 |
"
|
|
46 |
!
|
|
47 |
|
|
48 |
example
|
|
49 |
"
|
|
50 |
|s|
|
|
51 |
|
|
52 |
s := RecursiveSeriesStream on:[:n | n/2] startValue:1.
|
|
53 |
s next:10.
|
|
54 |
s next:10.
|
|
55 |
|
|
56 |
s := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:10.
|
|
57 |
s next:10.
|
|
58 |
s next:10.
|
|
59 |
"
|
|
60 |
!
|
|
61 |
|
|
62 |
example2
|
|
63 |
"
|
|
64 |
|s|
|
|
65 |
|
|
66 |
1 to:100 collect:[:n0 |
|
|
67 |
s := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:n0.
|
|
68 |
s next:10.
|
|
69 |
]
|
|
70 |
"
|
|
71 |
! !
|
|
72 |
|
|
73 |
!RecursiveSeriesStream class methodsFor:'instance creation'!
|
|
74 |
|
|
75 |
on:aBlock startValue:startValue
|
|
76 |
^ self basicNew initialize iterator:aBlock startValue:startValue
|
|
77 |
! !
|
|
78 |
|
|
79 |
!RecursiveSeriesStream methodsFor:'accessing'!
|
|
80 |
|
|
81 |
iterator:aOneArgBlock startValue:startValue
|
|
82 |
iterator := aOneArgBlock.
|
|
83 |
lastValue := startValue.
|
|
84 |
! !
|
|
85 |
|
|
86 |
!RecursiveSeriesStream methodsFor:'reading'!
|
|
87 |
|
|
88 |
next
|
|
89 |
"return the next element from the stream by evaluating the nextBlock"
|
|
90 |
|
|
91 |
|prevValue|
|
|
92 |
|
|
93 |
prevValue := lastValue.
|
|
94 |
lastValue := iterator value:lastValue.
|
|
95 |
^ prevValue
|
|
96 |
|
|
97 |
! !
|
|
98 |
|
|
99 |
!RecursiveSeriesStream class methodsFor:'documentation'!
|
|
100 |
|
|
101 |
version_CVS
|
|
102 |
^ '$Header$'
|
|
103 |
! !
|
|
104 |
|