--- a/LazyValue.st Sat Feb 09 16:00:47 2019 +0100
+++ b/LazyValue.st Sat Feb 09 16:57:28 2019 +0100
@@ -54,6 +54,99 @@
"
!
+example3
+ "an infinite list"
+
+ |gen infiniteList|
+
+ gen := [:n |
+ Cons
+ car:n
+ cdr:[ gen value:(n+1) ] lazyValue
+ ].
+
+ infiniteList := gen value:1.
+ 1 to:10 do:[:i |
+ Transcript showCR:infiniteList car.
+ infiniteList := infiniteList cdr.
+ ].
+ Transcript
+ show:'next 20 elements: ';
+ showCR:(infiniteList take:20) asArray.
+
+ "
+ self example3
+ "
+!
+
+example4
+ "an infinite list of primes"
+
+ |gen listOfPrimes isEmirp|
+
+ isEmirp := [:p | p printString reversed asNumber isPrime].
+
+ gen := [:n |
+ Cons
+ car:n
+ cdr:[ gen value:(n nextPrime) ] lazyValue
+ ].
+
+
+ listOfPrimes := gen value:2.
+ Transcript
+ show:'first 100 primes: ';
+ showCR:(listOfPrimes take:100) asArray.
+
+ "
+ self example4
+ "
+!
+
+example5
+ "an infinite list of emirps"
+
+ |primeGen listOfPrimes isEmirp emirpGen listOfEmirps |
+
+ isEmirp :=
+ [:p | |e|
+ (e := p printString reversed asNumber) isPrime
+ and:[ e ~= p ]
+ ].
+
+ primeGen :=
+ [:n |
+ LazyCons car:n cdr:[primeGen value:(n nextPrime)]
+ ].
+
+ emirpGen :=
+ [:l | |lR el|
+
+ lR := l.
+ [ el := lR car. lR := lR cdr. isEmirp value:el ] whileFalse.
+ LazyCons car:el cdr:[emirpGen value:lR]
+ ].
+
+ listOfPrimes := primeGen value:2.
+ listOfEmirps := emirpGen value:listOfPrimes.
+
+ Transcript
+ show:'first 20 emirps: ';
+ showCR:(listOfEmirps take:20) asArray.
+
+ Transcript
+ show:'emirps between 7700 and 8000 are: ';
+ showCR:((7700 to:8000) select:[:n | n isPrime and:[isEmirp value:n]]).
+
+ Transcript
+ show:'10000''th emirp: ';
+ showCR:(listOfEmirps nth:10000).
+
+ "
+ self example5
+ "
+!
+
examples
"
[exBegin]
@@ -199,8 +292,8 @@
examples2
"
Attention: for the examples below to work,
- you have to enable the Parsers AllowLazyValueExtension and
- the parsers AllowFunctionCallSyntaxForBlockEvaluation.
+ you have to enable the Parser's AllowLazyValueExtension
+ and the parser's AllowFunctionCallSyntaxForBlockEvaluation.
(set the corresponding classVariables in Parser to true).
Parser classVarAt:#AllowSTXSyntaxExtensions put:true.
@@ -401,7 +494,11 @@
!LazyValue class methodsFor:'instance creation'!
block:aBlock
+ "return a new lazyValue, which computes aBlock"
+
^ self new block:aBlock
+
+ "Modified (comment): / 09-02-2019 / 15:58:45 / Claus Gittinger"
! !
!LazyValue methodsFor:'printing'!