lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 2019-01-17 11:52 a.m., Albert Chan wrote:
function loop(e1,e2,e3)
    local i, n = -1, math.floor((e2-e1)/e3)
    if e1 + n * e3 == e2 then n = n+1 end
    return function()
        i = i + 1
        if i < n then return e1 + i*e3 end
    end
end
Uh ... above can be simplified:

function loop(e1, e2, e3)
     local i, n = -1, (e2-e1)/e3
     return function()
         i = i + 1
         if i <= n then return e1 + i*e3 end
     end
end


See also the implementation details of https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html