Created
October 14, 2014 18:16
-
-
Save marcoonroad/ca12dab24a7325fb64ff to your computer and use it in GitHub Desktop.
A better range generator function at Lua...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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