Skip to content

Instantly share code, notes, and snippets.

@ohmree
Created August 24, 2016 20:08
Show Gist options
  • Save ohmree/9606b830149db7780f89003ef5b2a42f to your computer and use it in GitHub Desktop.
Save ohmree/9606b830149db7780f89003ef5b2a42f to your computer and use it in GitHub Desktop.
Recursive & iterative map functions
arr = {}
function table_range(t, min, max)
for i = min, max do
table.insert(t, i, i)
i = i + 1
end
end
table_range(arr, 1, 1000000)
-- A map function using iteration
function map(fun, arr)
--index = index or 1
if (fun == nil and arr[1] == nil) then return nil end
local max = table.maxn(arr)
for i = 1, max do
fun(arr[i])
end
end
-- A map function using recursion
function rmap(fun, arr, index)
local index = index or 1
fun(arr[index])
if (fun ~= nil and arr[1] ~= nil and index ~= table.maxn(arr)) then
rmap(fun, arr, index + 1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment