Generator/Exponential: Difference between revisions

Add SenseTalk
m (→‎{{header|Raku}}: Fix-up some Perl6 -> Raku references)
(Add SenseTalk)
Line 3,185:
1024
1089</pre>
 
=={{header|SenseTalk}}==
The ExponentialGenerator script is a generator object for exponential values.
<lang sensetalk>// ExponentialGenerator.script
 
to initialize
set my base to 0
if my exponent is empty then set my exponent to 1 -- default if not given
end initialize
 
to handle nextValue
add 1 to my base
return my base to the power of my exponent
end nextValue</lang>
 
The FilteredGenerator takes source and filter generators. It gets values from the source generator but excludes those from the filter generator.
<lang sensetalk>// FilteredGenerator.script
 
// Takes a source generator, and a filter generator, which must both produce increasing values
// Produces values from the source generator that don't match values from the filter generator
 
to initialize
set my nextFilteredValue to the nextValue of my filter
end initialize
 
to handle nextValue
put the nextValue of my source into value -- get a candidate value
-- advance the filter as needed if it is behind
repeat while my nextFilteredValue is less than or equal to value
-- advance value if it's equal to the next filtered value
if my nextFilteredValue = value then set value to my source's nextValue
set my nextFilteredValue to my filter's nextValue
end repeat
return value
end nextValue
</lang>
 
This script shows the use of both of the generators.
<lang sensetalk>// Main.script to use the generators
 
set squares to new ExponentialGenerator with {exponent:2}
 
set cubes to new ExponentialGenerator with {exponent:3}
 
put "First 10 Squares:"
repeat 10 times
put squares.nextValue
end repeat
 
put "-" repeated 30 times
 
put "First 10 Cubes:"
repeat 10 times
put cubes.nextValue
end repeat
 
put "-" repeated 30 times
 
set filteredSquares to new FilteredGenerator with {
source: new ExponentialGenerator with {exponent:2},
filter: new ExponentialGenerator with {exponent:3}
}
 
repeat 20 times
get filteredSquares.nextValue
end repeat
 
put "Filtered Squares 21 to 30:"
repeat with n=21 to 30
put n & ":" && filteredSquares.nextValue
end repeat
</lang>
{{out}}
<pre>
First 10 Squares:
1
4
9
16
25
36
49
64
81
100
------------------------------
First 10 Cubes:
1
8
27
64
125
216
343
512
729
1000
------------------------------
Filtered Squares 21 to 30:
21: 529
22: 576
23: 625
24: 676
25: 784
26: 841
27: 900
28: 961
29: 1024
30: 1089
</pre>
 
=={{header|Sidef}}==