LazyArray.st
changeset 1300 e606c08de0a6
child 2713 235621fd5dff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyArray.st	Wed Aug 27 20:25:25 2003 +0200
@@ -0,0 +1,79 @@
+"{ Package: 'stx:libbasic2' }"
+
+ArrayedCollection variableSubclass:#LazyArray
+	instanceVariableNames:'valueGenerator'
+	classVariableNames:'UncomputedValue'
+	poolDictionaries:''
+	category:'Collections-Arrayed'
+!
+
+!LazyArray class methodsFor:'documentation'!
+
+documentation
+"
+    An Array which computes its value lazyly (on demand).
+
+    [author:]
+        Claus Gittinger (cg@alan)
+
+    [see also:]
+        Lazy
+"
+!
+
+examples
+"
+                                                                [exBegin]
+    |squares|
+
+    squares := LazyArray new:100.
+    squares valueGenerator:[:index | index squared].
+
+    squares at:50.   
+    squares inspect.
+                                                                [exEnd]
+"
+! !
+
+!LazyArray class methodsFor:'initialization'!
+
+initialize
+    UncomputedValue isNil ifTrue:[
+        UncomputedValue := Object new.
+    ]
+
+    "
+     self initialize
+    "
+! !
+
+!LazyArray class methodsFor:'instance creation'!
+
+new:size
+    ^ (super new:size) atAllPut:UncomputedValue
+! !
+
+!LazyArray methodsFor:'accessing'!
+
+at:index
+    |val|
+
+    val := super at:index.
+    val == UncomputedValue ifTrue:[
+        val := valueGenerator value:index.
+        self at:index put:val.
+    ].
+    ^ val.
+!
+
+valueGenerator:aBlock
+    valueGenerator := aBlock
+! !
+
+!LazyArray class methodsFor:'documentation'!
+
+version
+    ^ '$Header: /cvs/stx/stx/libbasic2/LazyArray.st,v 1.1 2003-08-27 18:25:25 cg Exp $'
+! !
+
+LazyArray initialize!