LazyArray.st
author Claus Gittinger <cg@exept.de>
Wed, 27 Aug 2003 20:25:25 +0200
changeset 1300 e606c08de0a6
child 2713 235621fd5dff
permissions -rw-r--r--
initial checkin

"{ 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!