2020-01-31 00:56:37 +00:00
|
|
|
--[[
|
|
|
|
sequence - functional + oo wrapper for ordered tables
|
|
|
|
|
|
|
|
sort of depends on functional.lua but can be used without it, will just
|
|
|
|
crash if you call the functional interface
|
|
|
|
|
|
|
|
in that case, you can still use the table methods that accept a table
|
|
|
|
first as method calls.
|
|
|
|
]]
|
2020-03-15 09:28:50 +00:00
|
|
|
|
2020-01-31 00:56:37 +00:00
|
|
|
local sequence = {}
|
|
|
|
|
2020-03-15 10:22:22 +00:00
|
|
|
sequence._mt = {__index = sequence}
|
2020-01-31 00:56:37 +00:00
|
|
|
|
2020-03-15 10:22:22 +00:00
|
|
|
--proxy missing table fns to global table api
|
|
|
|
sequence.__mt = {__index = table}
|
|
|
|
setmetatable(sequence, sequence.__mt)
|
2020-01-31 00:56:37 +00:00
|
|
|
|
2020-03-15 10:22:22 +00:00
|
|
|
--upgrade a table into a sequence, or create a new sequence
|
2020-01-31 00:56:37 +00:00
|
|
|
function sequence:new(t)
|
2020-03-15 10:22:22 +00:00
|
|
|
return setmetatable(t or {}, sequence._mt)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--alias
|
|
|
|
sequence.join = table.concat
|
2020-01-31 00:56:37 +00:00
|
|
|
|
|
|
|
--sorting default to stable if present
|
|
|
|
sequence.sort = table.stable_sort or table.sort
|
|
|
|
|
2020-03-15 09:28:50 +00:00
|
|
|
--(handle functional module delegation correctly)
|
|
|
|
local _func = BATTERIES_FUNCTIONAL_MODULE or table
|
|
|
|
|
|
|
|
--import functional interface to sequence in a type preserving way
|
2020-01-31 00:56:37 +00:00
|
|
|
function sequence:keys()
|
2020-03-15 09:28:50 +00:00
|
|
|
return sequence:new(_func.keys(self))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:values()
|
2020-03-15 09:28:50 +00:00
|
|
|
return sequence:new(_func.values(self))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:foreach(f)
|
2020-03-15 09:28:50 +00:00
|
|
|
return _func.foreach(self, f)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:reduce(f, o)
|
2020-03-15 09:28:50 +00:00
|
|
|
return _func.foreach(self, f, o)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:map(f)
|
2020-03-15 09:28:50 +00:00
|
|
|
return sequence:new(_func.map(self, f))
|
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:remap(f)
|
|
|
|
return _func.remap(self, f)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:filter(f)
|
2020-03-15 09:28:50 +00:00
|
|
|
return sequence:new(_func.filter(self, f))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:partition(f)
|
2020-03-15 09:28:50 +00:00
|
|
|
local a, b = _func.partition(self, f)
|
2020-01-31 00:56:37 +00:00
|
|
|
return sequence:new(a), sequence:new(b)
|
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:zip(other, f)
|
2020-03-15 09:28:50 +00:00
|
|
|
return sequence:new(_func.zip(self, other, f))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:dedupe()
|
2020-03-15 09:28:50 +00:00
|
|
|
return _func.dedupe(self)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:append_inplace(other)
|
2020-03-15 09:28:50 +00:00
|
|
|
return _func.append_inplace(self, other)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:append(other)
|
|
|
|
return sequence:new():append_inplace(self):append_inplace(other)
|
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:copy(deep)
|
2020-03-15 09:28:50 +00:00
|
|
|
return sequence:new(_func.copy(self, deep))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return sequence
|