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
|
2021-04-15 00:31:06 +00:00
|
|
|
--(common case where something returns another sequence for chaining)
|
|
|
|
for _, v in ipairs({
|
|
|
|
"keys",
|
|
|
|
"values",
|
|
|
|
"dedupe",
|
|
|
|
"collapse",
|
|
|
|
"append",
|
|
|
|
"overlay",
|
|
|
|
"copy",
|
|
|
|
}) do
|
|
|
|
local table_f = table[v]
|
|
|
|
sequence[v] = function(self, ...)
|
|
|
|
return sequence(table_f(self, ...))
|
|
|
|
end
|
2020-06-02 05:01:21 +00:00
|
|
|
end
|
|
|
|
|
2021-04-15 00:31:06 +00:00
|
|
|
--aliases
|
|
|
|
for _, v in ipairs({
|
|
|
|
{"flatten", "collapse"},
|
|
|
|
}) do
|
|
|
|
sequence[v[1]] = sequence[v[2]]
|
2020-05-14 05:28:38 +00:00
|
|
|
end
|
|
|
|
|
2021-04-15 00:21:41 +00:00
|
|
|
--import functional interface in method form
|
|
|
|
|
|
|
|
--(common case where something returns another sequence for chaining)
|
|
|
|
for _, v in ipairs({
|
|
|
|
"map",
|
|
|
|
"map_inplace",
|
|
|
|
"filter",
|
|
|
|
"filter_inplace",
|
|
|
|
"remove_if",
|
|
|
|
"zip",
|
|
|
|
"stitch",
|
|
|
|
"cycle",
|
|
|
|
}) do
|
|
|
|
local functional_f = functional[v]
|
|
|
|
sequence[v] = function(self, ...)
|
|
|
|
return sequence(functional_f(self, ...))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-15 00:31:06 +00:00
|
|
|
--(cases where we don't want to construct a new sequence)
|
|
|
|
for _, v in ipairs({
|
|
|
|
"foreach",
|
|
|
|
"reduce",
|
|
|
|
"any",
|
|
|
|
"none",
|
|
|
|
"all",
|
|
|
|
"count",
|
|
|
|
"contains",
|
|
|
|
"sum",
|
|
|
|
"mean",
|
|
|
|
"minmax",
|
|
|
|
"max",
|
|
|
|
"min",
|
|
|
|
"find_min",
|
|
|
|
"find_max",
|
|
|
|
"find_nearest",
|
|
|
|
"find_match",
|
|
|
|
}) do
|
|
|
|
sequence[v] = functional[v]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-15 00:21:41 +00:00
|
|
|
--aliases
|
|
|
|
for _, v in ipairs({
|
|
|
|
{"remap", "map_inplace"},
|
|
|
|
{"map_stitch", "stitch"},
|
|
|
|
{"map_cycle", "cycle"},
|
2021-04-15 00:31:06 +00:00
|
|
|
{"find_best", "find_max"},
|
2021-04-15 00:21:41 +00:00
|
|
|
}) do
|
|
|
|
sequence[v[1]] = sequence[v[2]]
|
|
|
|
end
|
|
|
|
|
2021-04-15 00:31:06 +00:00
|
|
|
--(anything that needs bespoke wrapping)
|
2020-01-31 00:56:37 +00:00
|
|
|
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
|
|
|
|
|
2020-06-02 05:01:21 +00:00
|
|
|
return sequence
|