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