June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,19 +1,26 @@
|
|||
{{omit from|Lilypond}}
|
||||
[[Category:Non parametric generators]]
|
||||
[[Category:Stateful transactions]]
|
||||
|
||||
A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided.
|
||||
|
||||
Generators are often built on top of coroutines or objects so that the internal state of the object is handled “naturally”.
|
||||
|
||||
Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.
|
||||
|
||||
'''Task description'''
|
||||
|
||||
# Create a function that returns a generation of the m'th powers of the positive integers starting from zero, in order, and without obvious or simple upper limit. (Any upper limit to the generator should not be stated in the source but should be down to factors such as the languages natural integer size limit or computational time/size).
|
||||
# Use it to create a generator of:
|
||||
:# Squares.
|
||||
:# Cubes.
|
||||
# Create a new generator that filters all cubes from the generator of squares.
|
||||
# Drop the first 20 values from this last generator of filtered results then show the next 10 values
|
||||
;Task:
|
||||
* Create a function that returns a generation of the m'th powers of the positive integers starting from zero, in order, and without obvious or simple upper limit. (Any upper limit to the generator should not be stated in the source but should be down to factors such as the languages natural integer size limit or computational time/size).
|
||||
* Use it to create a generator of:
|
||||
:::* Squares.
|
||||
:::* Cubes.
|
||||
* Create a new generator that filters all cubes from the generator of squares.
|
||||
* Drop the first 20 values from this last generator of filtered results, and then show the next 10 values.
|
||||
|
||||
|
||||
Note that this task ''requires'' the use of generators in the calculation of the result.
|
||||
|
||||
'''See also'''
|
||||
|
||||
;Also see:
|
||||
* [[wp:Generator (computer_science)|Generator]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,16 +1,20 @@
|
|||
drop(n, gen) = (for i=1:n gen() end ; gen)
|
||||
drop(gen::Function, n::Integer) = (for _ in 1:n gen() end; gen)
|
||||
take(gen::Function, n::Integer) = collect(gen() for _ in 1:n)
|
||||
|
||||
take(n, gen) = [gen() for i=1:n]
|
||||
|
||||
pgen(n) = let x=0; () -> (x+=1)^n; end
|
||||
|
||||
function genfilter(gen1, gen2)
|
||||
let r1 = -Inf, r2 = gen2()
|
||||
() -> begin
|
||||
r1 = gen1()
|
||||
while r2 < r1 r2 = gen2() end
|
||||
while r1 == r2 r1 = gen1() end
|
||||
r1
|
||||
end
|
||||
end
|
||||
function pgen(n::Number)
|
||||
x = 0
|
||||
return () -> (x += 1) ^ n
|
||||
end
|
||||
|
||||
function genfilter(g1::Function, g2::Function)
|
||||
local r1
|
||||
local r2 = g2()
|
||||
return () -> begin
|
||||
r1 = g1()
|
||||
while r2 < r1 r2 = g2() end
|
||||
while r1 == r2 r1 = g1() end
|
||||
return r1
|
||||
end
|
||||
end
|
||||
|
||||
@show take(drop(genfilter(pgen(2), pgen(3)), 20), 10)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
squares = script("generator.power").new(2)
|
||||
cubes = script("generator.power").new(3)
|
||||
filter = script("generator.filter").new(squares, cubes)
|
||||
filter.skip(20)
|
||||
res = []
|
||||
i = 0
|
||||
repeat while filter.exec(res)
|
||||
i = i + 1
|
||||
if i>10 then exit repeat
|
||||
put res[1]
|
||||
end repeat
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
property _exp
|
||||
property _index
|
||||
|
||||
-- @constructor
|
||||
on new (me, e)
|
||||
me._exp = e
|
||||
me._index = 0
|
||||
return me
|
||||
end
|
||||
|
||||
on exec (me, input)
|
||||
me._index = me._index+1
|
||||
input[1] = integer(power(me._index, me._exp))
|
||||
return TRUE
|
||||
end
|
||||
|
||||
on skip (me, steps)
|
||||
me._index = me._index + steps
|
||||
end
|
||||
|
||||
on reset (me)
|
||||
me._index = 0
|
||||
end
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
property _genv
|
||||
property _genf
|
||||
|
||||
-- @constructor
|
||||
on new (me, genv, genf)
|
||||
me._genv = genv
|
||||
me._genf = genf
|
||||
return me
|
||||
end
|
||||
|
||||
on exec (me, input)
|
||||
repeat while TRUE
|
||||
me._genv.exec(input)
|
||||
v = input[1]
|
||||
ok = TRUE
|
||||
me._genf.reset() -- reset filter generator
|
||||
repeat while TRUE
|
||||
me._genf.exec(input)
|
||||
f = input[1]
|
||||
if f>v then exit repeat
|
||||
if f=v then
|
||||
ok=FALSE
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
if ok then
|
||||
input[1] = v
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
return TRUE
|
||||
end
|
||||
|
||||
on skip (me, steps)
|
||||
repeat with i = 1 to steps
|
||||
me.exec([])
|
||||
end repeat
|
||||
end
|
||||
|
||||
on reset (me)
|
||||
me._genv.reset()
|
||||
me._genf.reset()
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue