eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'LazyValue':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: LazyValue


Inheritance:

   ProtoObject
   |
   +--LazyValue

Package:
stx:libbasic2
Category:
Kernel-Processes
Version:
rev: 1.13 date: 2019/06/25 09:06:22
user: cg
file: LazyValue.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger

Description:


I represent an expression which might not be needed.
Any messages sent to me will force the evaluation (once).
The value is remembered.

Can be used to simulate non-strict programming languages.


Related information:

    Block
    Future
    Lazy

Class protocol:

instance creation
o  block: aBlock
return a new lazyValue, which computes aBlock


Instance protocol:

printing
o  displayOn: aGCOrStream
notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
whereas printString and printOn: will wait (they are for the program to print data).

o  displayString
notice: displayString and displayOn: will not wait for the value (they are for developers and inspectors),
whereas printString and printOn: will wait (they are for the program to print data).

private access
o  block: aBlock

queries
o  class

synchronising
o  _evaluate_
ensure that block is only executed once

o  doesNotUnderstand: aMessage
enable debugging / inspecting without evaluating

o  perform: aSelector withArguments: argArray
send the message aSelector with all args taken from argArray
to the receiver.

testing
o  isBehavior

o  isLazyValue


Examples:


    |x|

    x := LazyValue block:[ Transcript flash. 1234 ].
    Delay waitForSeconds:4.
    Transcript showCR:x printString
listFrom:n - an infinite list of integers starting with n
    |gen infiniteList|

    gen := [:n |
                        Cons 
                            car:n 
                            cdr:( LazyValue block:[gen value:n+1] )
                   ].

    infiniteList := gen value:1.
    1 to:10 do:[:i |
        Transcript showCR:infiniteList car.
        infiniteList := infiniteList cdr.
    ].
filterOdd:l - an infinite list filtering odd numbers from another list
    |gen infiniteList genOdd infiniteOdd|

    gen := [:n |
                        Cons 
                            car:n 
                            cdr:( LazyValue block:[gen value:n+1] )
                   ].

    infiniteList := gen value:1.

    genOdd := [:l |
                        |lR el|

                        lR := l.
                        [ el := lR car. lR := lR cdr. el odd ] whileFalse:[].
                        Cons 
                            car:el 
                            cdr:( LazyValue block:[genOdd value:lR] )
                   ].

    infiniteOdd := genOdd value:infiniteList.

    1 to:10 do:[:i |
        Transcript showCR:infiniteOdd car.
        infiniteOdd := infiniteOdd cdr.
    ].
powersOf:n - an infinite list of powers of n
    |genPowersOf infiniteListOfPowers|

    genPowersOf := [:base |
                   |powersOfBase|

                   powersOfBase :=
                       [:n |
                            Cons 
                                car:n 
                                cdr:( LazyValue block:[powersOfBase value:n*base] )
                       ].
                   powersOfBase value:1.
                ].

    infiniteListOfPowers := genPowersOf value:2.
    1 to:10 do:[:i |
        Transcript showCR:infiniteListOfPowers car.
        infiniteListOfPowers := infiniteListOfPowers cdr.
    ].
merge2:a _:b - merge 2 lists.
    |genMerge2 l gen infiniteList genOdd infiniteOdd genEven infiniteEven genMerge|

    gen := [:n |
                        Cons 
                            car:n 
                            cdr:( LazyValue block:[gen value:n+1] )
                   ].

    infiniteList := gen value:1.

    genOdd := [:l |
                        |lR el|

                        lR := l.
                        [ el := lR car. lR := lR cdr. el odd ] whileFalse:[].
                        Cons 
                            car:el 
                            cdr:( LazyValue block:[genOdd value:lR] )
                   ].

    infiniteOdd := genOdd value:infiniteList.

    genEven := [:l |
                        |lR el|

                        lR := l.
                        [ el := lR car. lR := lR cdr. el even ] whileFalse:[].
                        Cons 
                            car:el 
                            cdr:( LazyValue block:[genEven value:lR] )
                   ].

    infiniteEven := genEven value:infiniteList.

    genMerge2 := [:a :b |
                   |nextA nextB|

                   nextA := a car.
                   nextB := b car.

                   nextA < nextB ifTrue:[
                       Cons 
                           car:nextA 
                           cdr:( LazyValue block:[genMerge2 value:(a cdr) value:b] )
                   ] ifFalse:[
                       Cons 
                           car:nextB 
                           cdr:( LazyValue block:[genMerge2 value:a value:(b cdr)] )
                   ].
                ].

    l := genMerge2 value:infiniteOdd value:infiniteEven.
    1 to:10 do:[:i |
        Transcript showCR:l car.
        l := l cdr.
    ].


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Sun, 03 Mar 2024 19:15:02 GMT