2020-01-31 00:56:37 +00:00
|
|
|
--[[
|
|
|
|
sequence - functional + oo wrapper for ordered tables
|
|
|
|
|
2020-05-14 05:28:38 +00:00
|
|
|
mainly beneficial when used for method chaining
|
|
|
|
to save on typing and data plumbing
|
2020-01-31 00:56:37 +00:00
|
|
|
]]
|
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-06-02 05:01:21 +00:00
|
|
|
local stable_sort = require(path .. "sort").stable_sort
|
2020-01-31 00:56:37 +00:00
|
|
|
|
2020-05-14 05:28:38 +00:00
|
|
|
local sequence = class(table) --proxy missing table fns to tablex api
|
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-06-02 05:01:21 +00:00
|
|
|
--sorting default to stable
|
|
|
|
sequence.sort = stable_sort
|
2020-01-31 00:56:37 +00:00
|
|
|
|
2020-05-14 05:28:38 +00:00
|
|
|
--patch various interfaces in a type-preserving way, for method chaining
|
|
|
|
|
|
|
|
--import copying tablex
|
2020-01-31 00:56:37 +00:00
|
|
|
function sequence:keys()
|
2020-06-02 05:01:21 +00:00
|
|
|
return sequence(table.keys(self))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:values()
|
2020-06-02 05:01:21 +00:00
|
|
|
return sequence(table.values(self))
|
2020-05-14 05:28:38 +00:00
|
|
|
end
|
|
|
|
|
2020-06-02 05:01:21 +00:00
|
|
|
function sequence:dedupe()
|
|
|
|
return sequence(table.dedupe(self))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
2020-07-02 07:05:26 +00:00
|
|
|
function sequence:collapse()
|
|
|
|
return sequence(table.collapse(self))
|
|
|
|
end
|
|
|
|
sequence.flatten = sequence.collapse
|
|
|
|
|
2020-06-02 05:01:21 +00:00
|
|
|
function sequence:append(...)
|
|
|
|
return sequence(table.append(self, ...))
|
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:overlay(...)
|
|
|
|
return sequence(table.overlay(self, ...))
|
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:copy(...)
|
|
|
|
return sequence(table.copy(self, ...))
|
2020-05-14 05:28:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--import functional interface
|
2020-01-31 00:56:37 +00:00
|
|
|
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-05-14 05:28:38 +00:00
|
|
|
return sequence(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-05-14 05:28:38 +00:00
|
|
|
return sequence(functional.filter(self, f))
|
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:remove_if(f)
|
|
|
|
return sequence(functional.remove_if(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-05-14 05:28:38 +00:00
|
|
|
return sequence(a), sequence(b)
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function sequence:zip(other, f)
|
2020-05-14 05:28:38 +00:00
|
|
|
return sequence(functional.zip(self, other, f))
|
2020-01-31 00:56:37 +00:00
|
|
|
end
|
|
|
|
|
2020-06-02 05:01:21 +00:00
|
|
|
return sequence
|