-
Notifications
You must be signed in to change notification settings - Fork 2
/
functional.lua
66 lines (58 loc) · 2.35 KB
/
functional.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--[[
#########################################################################
# #
# functional.lua #
# #
# Functional programming constructs #
# #
# Copyright 2011 Josh Bothun #
# http://minornine.com #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License <http://www.gnu.org/licenses/> for #
# more details. #
# #
#########################################################################
--]]
-- Remove objects from a lua table, defragmenting the table in the process.
function leaf.remove_if(t, cull)
-- Defrag
local size = #t
local free = 1
for i = 1, #t do
if not cull(t[i]) then
t[free] = t[i]
free = free + 1
end
end
-- Nil remainder
for i = free, size do
t[i] = nil
end
end
--- Map for 2D arrays
function leaf.map2d(array, callback)
for i=1, #array do
for j=1, #array[i] do
callback(array[i][j])
end
end
end
--- Like underscore pick
function leaf.pick(table, keys)
result = {}
for i, key in ipairs(keys) do
if table[key] ~= nil then
result[key] = table[key]
end
end
return result
end