1300
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
3845
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
1300
|
5 |
ArrayedCollection variableSubclass:#LazyArray
|
|
6 |
instanceVariableNames:'valueGenerator'
|
3306
|
7 |
classVariableNames:'UncomputedValueSingleton'
|
1300
|
8 |
poolDictionaries:''
|
|
9 |
category:'Collections-Arrayed'
|
|
10 |
!
|
|
11 |
|
3306
|
12 |
Object subclass:#UncomputedValue
|
|
13 |
instanceVariableNames:''
|
|
14 |
classVariableNames:''
|
|
15 |
poolDictionaries:''
|
|
16 |
privateIn:LazyArray
|
|
17 |
!
|
|
18 |
|
1300
|
19 |
!LazyArray class methodsFor:'documentation'!
|
|
20 |
|
|
21 |
documentation
|
|
22 |
"
|
3845
|
23 |
An Array which computes its value lazily (on demand) and remembers those values.
|
1300
|
24 |
|
|
25 |
[author:]
|
|
26 |
Claus Gittinger (cg@alan)
|
|
27 |
|
|
28 |
[see also:]
|
|
29 |
Lazy
|
|
30 |
"
|
|
31 |
!
|
|
32 |
|
|
33 |
examples
|
|
34 |
"
|
|
35 |
[exBegin]
|
|
36 |
|squares|
|
|
37 |
|
|
38 |
squares := LazyArray new:100.
|
|
39 |
squares valueGenerator:[:index | index squared].
|
|
40 |
|
|
41 |
squares at:50.
|
|
42 |
squares inspect.
|
|
43 |
[exEnd]
|
|
44 |
"
|
|
45 |
! !
|
|
46 |
|
|
47 |
!LazyArray class methodsFor:'initialization'!
|
|
48 |
|
|
49 |
initialize
|
3306
|
50 |
UncomputedValueSingleton isNil ifTrue:[
|
|
51 |
UncomputedValueSingleton := UncomputedValue new.
|
1300
|
52 |
]
|
|
53 |
|
|
54 |
"
|
|
55 |
self initialize
|
|
56 |
"
|
|
57 |
! !
|
|
58 |
|
|
59 |
!LazyArray class methodsFor:'instance creation'!
|
|
60 |
|
|
61 |
new:size
|
|
62 |
^ (super new:size) atAllPut:UncomputedValue
|
|
63 |
! !
|
|
64 |
|
|
65 |
!LazyArray methodsFor:'accessing'!
|
|
66 |
|
|
67 |
at:index
|
|
68 |
|val|
|
|
69 |
|
|
70 |
val := super at:index.
|
|
71 |
val == UncomputedValue ifTrue:[
|
|
72 |
val := valueGenerator value:index.
|
|
73 |
self at:index put:val.
|
|
74 |
].
|
|
75 |
^ val.
|
|
76 |
!
|
|
77 |
|
|
78 |
valueGenerator:aBlock
|
|
79 |
valueGenerator := aBlock
|
|
80 |
! !
|
|
81 |
|
|
82 |
!LazyArray class methodsFor:'documentation'!
|
|
83 |
|
|
84 |
version
|
3845
|
85 |
^ '$Header$'
|
1300
|
86 |
! !
|
|
87 |
|
3306
|
88 |
|
1300
|
89 |
LazyArray initialize!
|