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-04-07 03:49:10 +00:00
|
|
|
local path = (...):gsub("sequence", "")
|
|
|
|
local class = require(path .. "class")
|
|
|
|
local table = require(path .. "tablex") --shadow global table module
|
|
|
|
local functional = require(path .. "functional")
|
2020-01-31 00:56:37 +00:00
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
local sequence = class()
|
2020-03-15 10:22:22 +00:00
|
|
|
--proxy missing table fns to global table api
|
2020-04-07 03:49:10 +00:00
|
|
|
setmetatable(sequence, {__index = table})
|
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-04-07 03:49:10 +00:00
|
|
|
return self:init(t or {})
|
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-04-07 03:49:10 +00:00
|
|
|
--import functional interface to sequence in a type-preserving way, for method chaining
|
2020-01-31 00:56:37 +00:00
|
|
|
function sequence:keys()
|
2020-04-07 03:49:10 +00:00
|
|
|
return sequence:new(functional.keys(self))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:values()
|
2020-04-07 03:49:10 +00:00
|
|
|
return sequence:new(functional.values(self))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:foreach(f)
|
2020-04-07 03:49:10 +00:00
|
|
|
return functional.foreach(self, f)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:reduce(f, o)
|
2020-04-07 06:19:38 +00:00
|
|
|
return functional.reduce(self, f, o)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:map(f)
|
2020-04-07 03:49:10 +00:00
|
|
|
return sequence:new(functional.map(self, f))
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:remap(f)
|
2020-04-07 03:49:10 +00:00
|
|
|
return functional.remap(self, f)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:filter(f)
|
2020-04-07 03:49:10 +00:00
|
|
|
return sequence:new(functional.filter(self, f))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:partition(f)
|
2020-04-07 03:49:10 +00:00
|
|
|
local a, b = functional.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-04-07 03:49:10 +00:00
|
|
|
return sequence:new(functional.zip(self, other, f))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:dedupe()
|
2020-04-07 03:49:10 +00:00
|
|
|
return functional.dedupe(self)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:append_inplace(other)
|
2020-04-07 03:49:10 +00:00
|
|
|
return functional.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-04-07 03:49:10 +00:00
|
|
|
return sequence:new(functional.copy(self, deep))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return sequence
|