Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Created October 14, 2014 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcoonroad/ca12dab24a7325fb64ff to your computer and use it in GitHub Desktop.
Save marcoonroad/ca12dab24a7325fb64ff to your computer and use it in GitHub Desktop.
A better range generator function at Lua...
-- as multi-dispatch pseudo-code
--
-- range(init, final, step):
---- return
---- [ where init > final ]
--
-- range(final):
---- return 0, range(0 + 1, final, 1)
---- [ where final is Positive Integer ]
--
-- range(init, final):
---- return init, range(init + 1, final, 1)
---- [ where both are Positive Integers ]
--
-- range(init, final, step):
---- return init, range(init + step, final, step)
---- [ where all the args are Positive Integers ]
--
function range (i, f, s)
if not f then
f = i
i = 0
end
s = s or 1 -- 1 is default step
if i > f then
return
else
return i, range (i + s, f, s)
end
end
-- test
print (range (10)) --> from 0 to 10
print (range (15, 25)) --> from 15 to 25
print (range (10, 50, 5)) --> from 10 to 50, jumping over each 5 elements
-- end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment