-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add library of table manipulation functions
- Loading branch information
1 parent
ec5a43f
commit c809622
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--1 | ||
local function filter(tbl, f) | ||
if tbl == nil then return nil end | ||
if f == nil then return {} end | ||
local t = {} | ||
for k,v in pairs(tbl) do | ||
if f(v,k,tbl) then t[#t+1] = v end | ||
end | ||
return t | ||
end | ||
local function find(tbl, f) | ||
if tbl == nil then return nil end | ||
if f == nil then return nil end | ||
for k,v in pairs(tbl) do | ||
if f(v,k,tbl) then return v, k end | ||
end | ||
return nil | ||
end | ||
local function map(tbl, f) | ||
if tbl == nil then return nil end | ||
if f == nil then return tbl end | ||
local t = {} | ||
for k,v in pairs(tbl) do | ||
t[k] = f(v,k,tbl) | ||
end | ||
return t | ||
end | ||
local function reduce(tbl, f, initial) | ||
if tbl == nil then return nil end | ||
if f == nil then return tbl end | ||
local result | ||
local startIndex = 1 | ||
if initial then | ||
result = initial | ||
else | ||
result = tbl[1] | ||
startIndex = 2 | ||
end | ||
for i = startIndex,#tbl do | ||
result = f(result, tbl[i], i, tbl) | ||
end | ||
return result | ||
end | ||
|
||
return { | ||
filter = filter, | ||
find = find, | ||
map = map, | ||
reduce = reduce | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[{"name":"batmon.lua","version":4},{"name":"config-loader.lua","version":1},{"name":"door.lua","version":1},{"name":"installer.lua","version":9},{"name":"place-item.lua","version":3},{"name":"reactorcontrol.lua","version":5},{"name":"seq-client.lua","version":1},{"name":"signal-slider.lua","version":4},{"name":"turbinecontrol.lua","version":16},{"name":"websocket.lua","version":12}] | ||
[{"name":"batmon.lua","version":4},{"name":"config-loader.lua","version":1},{"name":"door.lua","version":1},{"name":"installer.lua","version":9},{"name":"lib/table.lua","version":1},{"name":"place-item.lua","version":3},{"name":"reactorcontrol.lua","version":5},{"name":"seq-client.lua","version":1},{"name":"signal-slider.lua","version":4},{"name":"turbinecontrol.lua","version":16},{"name":"websocket.lua","version":12}] |