lua-users home
lua-l archive

Re: [NoW] About numeric for-loop

[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

AltStyle によって変換されたページ (->オリジナル) /