Examples - salmito/leda GitHub Wiki
Pipelined sieve of Eratosthenes
require "leda"
local maxN=maxN or 500
local max_primes_per_stage=max_primes or 10
local conn={}
local counter=leda.stage{
handler=function(prime)
counter = counter or 0 -- initialize counter
counter = counter+1 -- increment counter
if counter==maxN then --We reached the prime limit
leda.quit()
end
end,
stateful=true,
name="Prime Counter"
}
local init_stage=leda.stage{"Odd number generator",
handler=function ()
leda.send('prime',2)
local i = 3
while true do
leda.send('val',i)
i=i+2
end
end
}
local last_stage=init_stage
table.insert(conn,init_stage:connect("prime",counter))
--creating stages
for i=1,maxN do
local s=leda.stage{
handler=function (val)
if not prime then
prime=val
leda.send('prime',val)
return
end
if val%prime ~= 0 then --Not divisible
leda.send('val',val)
end
end,
stateful=true
}
table.insert(conn,last_stage:connect('val',s))
table.insert(conn,s:connect('prime',counter))
last_stage=s
end
local g=leda.graph(conn)
init_stage:send()
g:run()