Skip to content

Instantly share code, notes, and snippets.

@qi7chen
Forked from anonymous/functional.lua
Last active August 29, 2015 14:10
Show Gist options
  • Save qi7chen/fbcf90c6b3c04c54c3f6 to your computer and use it in GitHub Desktop.
Save qi7chen/fbcf90c6b3c04c54c3f6 to your computer and use it in GitHub Desktop.
-- map(table, function)
-- e.g: map({1,2,3}, function(a) return a*2 end) -> {2,4,6}
function map(tbl, func)
local newtbl = {}
for i,v in pairs(tbl) do
newtbl[i] = func(v)
end
return newtbl
end
-- filter(table, function)
-- e.g: filter({1,2,3,4}, function(a) return a%2 == 0 end) -> {2,4}
function filter(tbl, func)
local newtbl = {}
for i,v in pairs(tbl) do
if func(v) then
newtbl[i] = v
end
end
return newtbl
end
-- foldr(table, default_value, function)
-- e.g: foldr({1,2,3,4,5}, 1, function(a, b) return a*b end) -> 120
local function foldr(tbl, val, func)
for i, v in pairs(tbl) do
val = func(val, v)
end
return val
end
-- reduce(table, function)
-- e.g: reduce({1,2,3,4}, function(a, b) return a+b end) -> 10
function reduce(tbl, func)
local head = tbl[1]
local tail = {unpack(tbl, 2)}
return foldr(tail, head, func)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment