2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
|
|
|
functional programming facilities
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
notes:
|
|
|
|
be careful about creating closures in hot loops.
|
|
|
|
this is this module's achilles heel - there's no special
|
|
|
|
syntax for closures so it's not apparent that you're suddenly
|
|
|
|
allocating at every call
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
reduce has a similar problem, but at least arguments
|
|
|
|
there are clear!
|
|
|
|
]]
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
local path = (...):gsub("functional", "")
|
|
|
|
local tablex = require(path .. "tablex")
|
2021-04-12 03:04:08 +00:00
|
|
|
local mathx = require(path .. "mathx")
|
2020-04-07 03:49:10 +00:00
|
|
|
|
|
|
|
local functional = setmetatable({}, {
|
|
|
|
__index = tablex,
|
|
|
|
})
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2020-06-02 05:17:50 +00:00
|
|
|
--the identity function
|
|
|
|
function functional.identity(v)
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--simple sequential iteration, f is called for all elements of t
|
|
|
|
--f can return non-nil to break the loop (and return the value)
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.foreach(t, f)
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
local result = f(t[i], i)
|
|
|
|
if result ~= nil then
|
|
|
|
return result
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-11 02:38:52 +00:00
|
|
|
--performs a left to right reduction of t using f, with seed as the initial value
|
2020-11-10 09:51:43 +00:00
|
|
|
-- reduce({1, 2, 3}, 0, f) -> f(f(f(0, 1), 2), 3)
|
2020-01-29 03:26:28 +00:00
|
|
|
-- (but performed iteratively, so no stack smashing)
|
2020-11-10 09:51:43 +00:00
|
|
|
function functional.reduce(t, seed, f)
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
seed = f(seed, t[i], i)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2020-11-10 09:51:43 +00:00
|
|
|
return seed
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--maps a sequence {a, b, c} -> {f(a), f(b), f(c)}
|
2020-11-10 09:51:43 +00:00
|
|
|
-- (automatically drops any nils to keep a sequence, so can be used to simultaneously map and filter)
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.map(t, f)
|
2020-11-10 09:51:43 +00:00
|
|
|
local result = {}
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
local v = f(t[i], i)
|
|
|
|
if v ~= nil then
|
|
|
|
table.insert(result, v)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
end
|
2020-11-10 09:51:43 +00:00
|
|
|
return result
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--maps a sequence inplace, modifying it {a, b, c} -> {f(a), f(b), f(c)}
|
2020-11-10 09:51:43 +00:00
|
|
|
-- (automatically drops any nils, which can be used to simultaneously map and filter)
|
|
|
|
function functional.map_inplace(t, f)
|
|
|
|
local write_i = 0
|
2020-11-11 02:38:52 +00:00
|
|
|
local n = #t --cache, so splitting the sequence doesn't stop iteration
|
2020-11-10 09:51:43 +00:00
|
|
|
for i = 1, n do
|
|
|
|
local v = f(t[i], i)
|
|
|
|
if v ~= nil then
|
|
|
|
write_i = write_i + 1
|
|
|
|
t[write_i] = v
|
|
|
|
end
|
|
|
|
if i ~= write_i then
|
|
|
|
t[i] = nil
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2020-11-10 09:51:43 +00:00
|
|
|
--alias
|
|
|
|
functional.remap = functional.map_inplace
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--filters a sequence
|
2020-11-10 09:51:43 +00:00
|
|
|
-- returns a table containing items where f(v, i) returns truthy
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.filter(t, f)
|
2020-11-10 09:51:43 +00:00
|
|
|
local result = {}
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-12 02:50:30 +00:00
|
|
|
local v = t[i]
|
|
|
|
if f(v, i) then
|
2020-11-10 09:51:43 +00:00
|
|
|
table.insert(result, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
--filters a sequence in place, modifying it
|
|
|
|
function functional.filter_inplace(t, f)
|
|
|
|
local write_i = 1
|
2020-11-11 02:38:52 +00:00
|
|
|
local n = #t --cache, so splitting the sequence doesn't stop iteration
|
2020-11-10 09:51:43 +00:00
|
|
|
for i = 1, n do
|
|
|
|
local v = t[i]
|
2020-01-29 03:26:28 +00:00
|
|
|
if f(v, i) then
|
2020-11-10 09:51:43 +00:00
|
|
|
t[write_i] = v
|
|
|
|
write_i = write_i + 1
|
|
|
|
end
|
|
|
|
if i ~= write_i then
|
|
|
|
t[i] = nil
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2020-05-11 23:42:06 +00:00
|
|
|
-- complement of filter
|
|
|
|
-- returns a table containing items where f(v) returns falsey
|
|
|
|
-- nil results are included so that this is an exact complement of filter; consider using partition if you need both!
|
|
|
|
function functional.remove_if(t, f)
|
2020-11-10 09:51:43 +00:00
|
|
|
local result = {}
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-12 02:50:30 +00:00
|
|
|
local v = t[i]
|
|
|
|
if not f(v, i) then
|
2020-11-10 09:51:43 +00:00
|
|
|
table.insert(result, v)
|
2020-05-11 00:41:27 +00:00
|
|
|
end
|
|
|
|
end
|
2020-11-10 09:51:43 +00:00
|
|
|
return result
|
2020-05-11 00:41:27 +00:00
|
|
|
end
|
|
|
|
|
2020-05-11 23:42:06 +00:00
|
|
|
--partitions a sequence into two, based on filter criteria
|
|
|
|
--simultaneous filter and remove_if
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.partition(t, f)
|
2020-01-29 03:26:28 +00:00
|
|
|
local a = {}
|
|
|
|
local b = {}
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-12 02:50:30 +00:00
|
|
|
local v = t[i]
|
|
|
|
if f(v, i) then
|
2020-01-29 03:26:28 +00:00
|
|
|
table.insert(a, v)
|
|
|
|
else
|
|
|
|
table.insert(b, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return a, b
|
|
|
|
end
|
|
|
|
|
2020-05-11 23:42:06 +00:00
|
|
|
-- returns a table where the elements in t are grouped into sequential tables by the result of f on each element.
|
2020-11-10 09:51:43 +00:00
|
|
|
-- more general than partition, but requires you to know your groups ahead of time
|
|
|
|
-- (or use numeric grouping and pre-seed) if you want to avoid pairs!
|
2020-05-11 23:42:06 +00:00
|
|
|
function functional.group_by(t, f)
|
|
|
|
local result = {}
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-12 02:50:30 +00:00
|
|
|
local v = t[i]
|
|
|
|
local group = f(v, i)
|
2020-05-11 23:42:06 +00:00
|
|
|
if result[group] == nil then
|
|
|
|
result[group] = {}
|
|
|
|
end
|
|
|
|
table.insert(result[group], v)
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--zips two sequences together into a new table, based on another function
|
|
|
|
--iteration limited by min(#t1, #t2)
|
|
|
|
--function receives arguments (t1, t2, i)
|
|
|
|
--nil results ignored
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.zip(t1, t2, f)
|
2020-01-29 03:26:28 +00:00
|
|
|
local ret = {}
|
2020-05-13 11:08:23 +00:00
|
|
|
local limit = math.min(#t1, #t2)
|
|
|
|
for i = 1, limit do
|
2020-01-29 03:26:28 +00:00
|
|
|
local v1 = t1[i]
|
|
|
|
local v2 = t2[i]
|
|
|
|
local zipped = f(v1, v2, i)
|
|
|
|
if zipped ~= nil then
|
|
|
|
table.insert(ret, zipped)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2021-04-12 03:04:08 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
--specialised maps
|
|
|
|
-- (experimental: let me know if you have better names for these!)
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
--maps a sequence {a, b, c} -> collapse { f(a), f(b), f(c) }
|
|
|
|
-- (ie results from functions should generally be sequences,
|
|
|
|
-- which are appended onto each other, resulting in one big sequence)
|
|
|
|
-- (automatically drops any nils, same as map)
|
|
|
|
function functional.stitch(t, f)
|
|
|
|
local result = {}
|
|
|
|
for i, v in ipairs(t) do
|
|
|
|
local v = f(v, i)
|
|
|
|
if v ~= nil then
|
|
|
|
if type(v) == "table" then
|
|
|
|
for _, e in ipairs(v) do
|
|
|
|
table.insert(result, e)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
table.insert(result, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
--alias
|
|
|
|
functional.map_stitch = functional.stitch
|
|
|
|
|
|
|
|
--maps a sequence {a, b, c} -> { f(a, b), f(b, c), f(c, a) }
|
|
|
|
-- useful for inter-dependent data
|
|
|
|
-- (automatically drops any nils, same as map)
|
|
|
|
|
|
|
|
function functional.cycle(t, f)
|
|
|
|
local result = {}
|
|
|
|
for i, a in ipairs(t) do
|
|
|
|
local b = t[mathx.wrap(i + 1, 1, #t)]
|
|
|
|
local v = f(a, b, i)
|
|
|
|
if v ~= nil then
|
|
|
|
table.insert(result, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
functional.map_cycle = functional.cycle
|
|
|
|
|
|
|
|
|
2020-05-13 11:09:15 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
--generating data
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
--generate data into a table
|
|
|
|
--basically a map on numeric values from 1 to count
|
|
|
|
--nil values are omitted in the result, as for map
|
|
|
|
function functional.generate(count, f)
|
2020-11-10 09:51:43 +00:00
|
|
|
local result = {}
|
2020-05-13 11:09:15 +00:00
|
|
|
for i = 1, count do
|
|
|
|
local v = f(i)
|
|
|
|
if v ~= nil then
|
2020-11-10 09:51:43 +00:00
|
|
|
table.insert(result, v)
|
2020-05-13 11:09:15 +00:00
|
|
|
end
|
|
|
|
end
|
2020-11-10 09:51:43 +00:00
|
|
|
return result
|
2020-05-13 11:09:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--2d version of the above
|
|
|
|
--note: ends up with a 1d table;
|
2020-06-02 05:17:50 +00:00
|
|
|
-- if you need a 2d table, you should nest 1d generate calls
|
2020-05-13 11:09:15 +00:00
|
|
|
function functional.generate_2d(width, height, f)
|
2020-11-10 09:51:43 +00:00
|
|
|
local result = {}
|
2020-05-13 11:09:15 +00:00
|
|
|
for y = 1, height do
|
|
|
|
for x = 1, width do
|
|
|
|
local v = f(x, y)
|
|
|
|
if v ~= nil then
|
2020-11-10 09:51:43 +00:00
|
|
|
table.insert(result, v)
|
2020-05-13 11:09:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
-----------------------------------------------------------
|
|
|
|
--common queries and reductions
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
--true if any element of the table matches f
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.any(t, f)
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
if f(t[i], i) then
|
2020-01-29 03:26:28 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--true if no element of the table matches f
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.none(t, f)
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
if f(t[i], i) then
|
2020-01-29 03:26:28 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--true if all elements of the table match f
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.all(t, f)
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
if not f(t[i], i) then
|
2020-01-29 03:26:28 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--counts the elements of t that match f
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.count(t, f)
|
2020-01-29 03:26:28 +00:00
|
|
|
local c = 0
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
if f(t[i], i) then
|
2020-01-29 03:26:28 +00:00
|
|
|
c = c + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
|
|
|
|
--true if the table contains element e
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.contains(t, e)
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
if t[i] == e then
|
2020-01-29 03:26:28 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--return the numeric sum of all elements of t
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.sum(t)
|
2020-11-10 09:51:43 +00:00
|
|
|
local c = 0
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
c = c + t[i]
|
|
|
|
end
|
|
|
|
return c
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--return the numeric mean of all elements of t
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.mean(t)
|
2020-01-29 03:26:28 +00:00
|
|
|
local len = #t
|
|
|
|
if len == 0 then
|
|
|
|
return 0
|
|
|
|
end
|
2020-04-07 03:49:10 +00:00
|
|
|
return functional.sum(t) / len
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--return the minimum and maximum of t in one pass
|
2020-03-15 09:28:50 +00:00
|
|
|
--or zero for both if t is empty
|
|
|
|
-- (would perhaps more correctly be math.huge, -math.huge
|
|
|
|
-- but that tends to be surprising/annoying in practice)
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.minmax(t)
|
2020-11-10 09:51:43 +00:00
|
|
|
local n = #t
|
|
|
|
if n == 0 then
|
|
|
|
return 0, 0
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
2020-11-10 09:51:43 +00:00
|
|
|
local max = t[1]
|
|
|
|
local min = t[1]
|
|
|
|
for i = 2, n do
|
2020-11-12 02:50:30 +00:00
|
|
|
local v = t[i]
|
2020-11-10 09:51:43 +00:00
|
|
|
min = math.min(min, v)
|
|
|
|
max = math.max(max, v)
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
|
|
|
return min, max
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--return the maximum element of t or zero if t is empty
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.max(t)
|
|
|
|
local min, max = functional.minmax(t)
|
2020-01-29 03:26:28 +00:00
|
|
|
return max
|
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--return the minimum element of t or zero if t is empty
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.min(t)
|
|
|
|
local min, max = functional.minmax(t)
|
2020-01-29 03:26:28 +00:00
|
|
|
return min
|
|
|
|
end
|
|
|
|
|
2020-04-04 08:59:40 +00:00
|
|
|
--return the element of the table that results in the lowest numeric value
|
|
|
|
--(function receives element and index respectively)
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.find_min(t, f)
|
2020-04-04 08:59:40 +00:00
|
|
|
local current = nil
|
2020-04-07 03:49:10 +00:00
|
|
|
local current_min = math.huge
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
local e = t[i]
|
2020-04-07 03:49:10 +00:00
|
|
|
local v = f(e, i)
|
|
|
|
if v and v < current_min then
|
|
|
|
current_min = v
|
2020-04-04 08:59:40 +00:00
|
|
|
current = e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return current
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--return the element of the table that results in the greatest numeric value
|
2020-04-04 08:59:40 +00:00
|
|
|
--(function receives element and index respectively)
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.find_max(t, f)
|
2020-01-29 03:26:28 +00:00
|
|
|
local current = nil
|
2020-04-07 03:49:10 +00:00
|
|
|
local current_max = -math.huge
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
local e = t[i]
|
2020-04-07 03:49:10 +00:00
|
|
|
local v = f(e, i)
|
|
|
|
if v and v > current_max then
|
|
|
|
current_max = v
|
2020-01-29 03:26:28 +00:00
|
|
|
current = e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return current
|
|
|
|
end
|
|
|
|
|
2020-04-04 08:59:40 +00:00
|
|
|
--alias
|
2020-04-07 03:49:10 +00:00
|
|
|
functional.find_best = functional.find_max
|
2020-04-04 08:59:40 +00:00
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--return the element of the table that results in the value nearest to the passed value
|
2020-11-10 09:51:43 +00:00
|
|
|
--todo: optimise, inline as this generates a closure each time
|
2020-11-12 02:50:30 +00:00
|
|
|
function functional.find_nearest(t, f, target)
|
|
|
|
local current = nil
|
|
|
|
local current_min = math.huge
|
|
|
|
for i = 1, #t do
|
|
|
|
local e = t[i]
|
|
|
|
local v = math.abs(f(e, i) - target)
|
|
|
|
if v and v < current_min then
|
|
|
|
current_min = v
|
|
|
|
current = e
|
|
|
|
if v == 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return current
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--return the first element of the table that results in a true filter
|
2020-04-07 03:49:10 +00:00
|
|
|
function functional.find_match(t, f)
|
2020-11-11 02:38:52 +00:00
|
|
|
for i = 1, #t do
|
2020-11-10 09:51:43 +00:00
|
|
|
local v = t[i]
|
2020-01-29 03:26:28 +00:00
|
|
|
if f(v) then
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
2020-03-15 09:28:50 +00:00
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
return functional
|