131 "Evaluates the factorial, starting only when the |
131 "Evaluates the factorial, starting only when the |
132 result is actually required (when printString is sent)." |
132 result is actually required (when printString is sent)." |
133 |
133 |
134 | fac | |
134 | fac | |
135 fac := [100 factorial] futureValue. |
135 fac := [100 factorial] futureValue. |
136 Transcript showCr: 'Doing nothing. '. |
136 Transcript showCR: 'Doing nothing. '. |
137 (Delay forSeconds: 2) wait. |
137 (Delay forSeconds: 2) wait. |
138 Transcript showCr: fac printString |
138 Transcript showCR: fac printString |
139 |
139 |
140 "Lazy example1"! |
140 "Lazy example1"! |
141 |
141 |
142 example2 |
142 example2 |
143 "Starts evaluating both factorials only when required (by the touch), |
143 "Starts evaluating both factorials only when required (by the touch), |
144 and waits until both blocks have finished before continuing." |
144 and waits until both blocks have finished before continuing." |
145 |
145 |
146 | fac1 fac2 | |
146 | fac1 fac2 | |
147 fac1 := [Transcript showCr: 'Starting fac1.. '. 100 factorial] lazyValue. |
147 fac1 := [Transcript showCR: 'Starting fac1.. '. 100 factorial] lazyValue. |
148 fac2 := [Transcript showCr: 'Starting fac2.. '. 120 factorial] lazyValue. |
148 fac2 := [Transcript showCR: 'Starting fac2.. '. 120 factorial] lazyValue. |
149 fac2 touch. |
149 fac2 touch. |
150 fac1 touch. |
150 fac1 touch. |
151 Transcript showCr: 'both completed.'. |
151 Transcript showCR: 'both completed.'. |
152 |
152 |
153 "Lazy example2"! |
153 "Lazy example2"! |
154 |
154 |
155 example3 |
155 example3 |
156 "Demonstrates how to pass arguments to a lazy evaluation block." |
156 "Demonstrates how to pass arguments to a lazy evaluation block." |
157 |
157 |
158 | temp | |
158 | temp | |
159 temp := [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4). |
159 temp := [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4). |
160 Transcript showCr: temp printString. |
160 Transcript showCR: temp printString. |
161 |
161 |
162 "Lazy example3"! ! |
162 "Lazy example3"! ! |
163 |
163 |
164 Lazy initialize! |
164 Lazy initialize! |
165 |
165 |