5259
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5257
|
3 |
"{ Package: 'stx:libbasic2' }"
|
|
4 |
|
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
|
7 |
IteratorStream subclass:#RecursiveSeriesStream
|
5261
|
8 |
instanceVariableNames:'lastValue endValue'
|
5257
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'Streams'
|
|
12 |
!
|
|
13 |
|
|
14 |
!RecursiveSeriesStream class methodsFor:'documentation'!
|
|
15 |
|
|
16 |
documentation
|
|
17 |
"
|
|
18 |
documentation to be added.
|
|
19 |
|
|
20 |
class:
|
|
21 |
<a short class summary here, describing what instances represent>
|
|
22 |
|
|
23 |
responsibilities:
|
|
24 |
<describing what my main role is>
|
|
25 |
|
|
26 |
collaborators:
|
|
27 |
<describing with whom and how I talk to>
|
|
28 |
|
|
29 |
API:
|
|
30 |
<public api and main messages>
|
|
31 |
|
|
32 |
example:
|
|
33 |
<a one-line examples on how to use - can also be in a separate example method>
|
|
34 |
|
|
35 |
implementation:
|
|
36 |
<implementation points>
|
|
37 |
|
|
38 |
[author:]
|
|
39 |
cg
|
|
40 |
|
|
41 |
[instance variables:]
|
|
42 |
|
|
43 |
[class variables:]
|
|
44 |
|
|
45 |
[see also:]
|
|
46 |
|
|
47 |
"
|
|
48 |
!
|
|
49 |
|
|
50 |
example
|
|
51 |
"
|
5259
|
52 |
|collatz|
|
5257
|
53 |
|
5259
|
54 |
collatz := [:n | RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[n*3+1]] startValue:n].
|
|
55 |
(collatz[20]) next:20.
|
|
56 |
(collatz[24]) next:20.
|
|
57 |
(collatz[25]) next:30.
|
5257
|
58 |
"
|
|
59 |
!
|
|
60 |
|
|
61 |
example2
|
|
62 |
"
|
5259
|
63 |
|max bits collatz newNums|
|
5257
|
64 |
|
5259
|
65 |
newNums := OrderedCollection new.
|
|
66 |
bits := SortedCollection new.
|
|
67 |
max := 2.
|
5262
|
68 |
collatz := RecursiveSeriesStream on:[:n | n even ifTrue:[n/2] ifFalse:[(n*3+1)/2]] startValue:1.
|
5259
|
69 |
1 to:100000 do:[:n0 |
|
|
70 |
newNums removeAll.
|
|
71 |
collatz startValue:n0.
|
|
72 |
[
|
|
73 |
|n|
|
|
74 |
n := collatz next.
|
|
75 |
newNums add:n.
|
|
76 |
n <= max
|
|
77 |
] whileFalse.
|
|
78 |
bits addAll:newNums.
|
|
79 |
[bits notEmpty and:[bits first <= (max+1)]] whileTrue:[max := max max:(bits first). bits removeFirst].
|
|
80 |
].
|
|
81 |
self halt.
|
5257
|
82 |
"
|
|
83 |
! !
|
|
84 |
|
|
85 |
!RecursiveSeriesStream class methodsFor:'instance creation'!
|
|
86 |
|
|
87 |
on:aBlock startValue:startValue
|
|
88 |
^ self basicNew initialize iterator:aBlock startValue:startValue
|
|
89 |
! !
|
|
90 |
|
|
91 |
!RecursiveSeriesStream methodsFor:'accessing'!
|
|
92 |
|
5261
|
93 |
atEnd
|
|
94 |
^ lastValue = endValue
|
|
95 |
!
|
|
96 |
|
|
97 |
endValue:something
|
|
98 |
"define a stop value"
|
|
99 |
|
|
100 |
endValue := something.
|
|
101 |
!
|
|
102 |
|
5257
|
103 |
iterator:aOneArgBlock startValue:startValue
|
|
104 |
iterator := aOneArgBlock.
|
|
105 |
lastValue := startValue.
|
5259
|
106 |
!
|
|
107 |
|
|
108 |
startValue:startValue
|
|
109 |
lastValue := startValue.
|
5257
|
110 |
! !
|
|
111 |
|
|
112 |
!RecursiveSeriesStream methodsFor:'reading'!
|
|
113 |
|
|
114 |
next
|
|
115 |
"return the next element from the stream by evaluating the nextBlock"
|
|
116 |
|
|
117 |
|prevValue|
|
|
118 |
|
|
119 |
prevValue := lastValue.
|
|
120 |
lastValue := iterator value:lastValue.
|
|
121 |
^ prevValue
|
|
122 |
|
|
123 |
! !
|
|
124 |
|
|
125 |
!RecursiveSeriesStream class methodsFor:'documentation'!
|
|
126 |
|
|
127 |
version_CVS
|
|
128 |
^ '$Header$'
|
|
129 |
! !
|
|
130 |
|