Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Apr 24, 2011
1 parent 49554f2 commit c3e78cc
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ THE SOFTWARE.

local assert, type = assert, type
local pairs, ipairs = pairs, ipairs
local min = math.min
local min, math_huge = math.min, math.huge
module(...)

functions = {}
Expand All @@ -36,9 +36,8 @@ function update(dt)
delay = delay - dt
if delay <= 0 then
to_remove[#to_remove+1] = func
else
functions[func] = delay
end
functions[func] = delay
end
for _,func in ipairs(to_remove) do
functions[func] = nil
Expand All @@ -53,10 +52,15 @@ end

function addPeriodic(delay, func, count)
assert(type(func) == "function", "second argument needs to be a function")
if count then
return add(delay, function(f) func(func) count = count - 1 if count > 0 then add(delay, f) end end)
end
return add(delay, function(f) func(func) add(delay, f) end)
local count = count or math_huge -- exploit below: math.huge - 1 = math.huge

add(delay, function(f)
func(func)
count = count - 1
if count > 0 then
add(delay, f)
end
end)
end

function clear()
Expand All @@ -76,8 +80,7 @@ function Oscillator(length, func)
assert(type(func) == "function", "second argument needs to be a function")
local t = 0
return function(dt, ...)
t = t + dt
while t > length do t = t - length end
t = (t + dt) % length
return func(t/length, ...)
end
end

0 comments on commit c3e78cc

Please sign in to comment.