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