[modified] sequence wrappers a bit more; exposed functional query interface

This commit is contained in:
Max Cahill 2021-04-15 10:31:06 +10:00
parent 39268148d8
commit 87834e9267

View File

@ -24,33 +24,27 @@ sequence.sort = stable_sort
--patch various interfaces in a type-preserving way, for method chaining
--import copying tablex
function sequence:keys()
return sequence(table.keys(self))
--(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
end
function sequence:values()
return sequence(table.values(self))
end
function sequence:dedupe()
return sequence(table.dedupe(self))
end
function sequence:collapse()
return sequence(table.collapse(self))
end
sequence.flatten = sequence.collapse
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, ...))
--aliases
for _, v in ipairs({
{"flatten", "collapse"},
}) do
sequence[v[1]] = sequence[v[2]]
end
--import functional interface in method form
@ -72,24 +66,40 @@ for _, v in ipairs({
end
end
--(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
--aliases
for _, v in ipairs({
{"remap", "map_inplace"},
{"map_stitch", "stitch"},
{"map_cycle", "cycle"},
{"find_best", "find_max"},
}) do
sequence[v[1]] = sequence[v[2]]
end
--(less common cases where we don't want to construct a new sequence or have more than one return value)
function sequence:foreach(f)
return functional.foreach(self, f)
end
function sequence:reduce(seed, f)
return functional.reduce(self, seed, f)
end
--(anything that needs bespoke wrapping)
function sequence:partition(f)
local a, b = functional.partition(self, f)
return sequence(a), sequence(b)