[checked revision] | [checked revision] |
((via Mediawiker ST3)) |
([ST]) |
||
(35 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
end |
end |
||
return tbl |
return tbl |
||
+ | end |
||
+ | |||
+ | function p.apply(tbl, f, ...) |
||
+ | for k, v in pairs(tbl) do |
||
+ | f(v, ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.arrayApply(tbl, f, ...) |
||
+ | for k, v in ipairs(tbl) do |
||
+ | f(v, ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.multiParamSafe(tbl, f, ...) |
||
+ | if not tbl or #tbl == 0 or #tbl[1] == 0 then return end |
||
+ | local util_vars = require("Module:VarsUtil") |
||
+ | local new = util_table.interlace(tbl) |
||
+ | util_vars.log(tbl) |
||
+ | util_vars.log('********') |
||
+ | util_vars.log(new) |
||
+ | for i, v in ipairs(new) do |
||
+ | new[i] = f(unpack(new[i])) |
||
+ | end |
||
+ | return new |
||
end |
end |
||
Line 44: | Line 71: | ||
util_table.removeFalseEntries(tbl2) |
util_table.removeFalseEntries(tbl2) |
||
for k, v in pairs(tbl2) do |
for k, v in pairs(tbl2) do |
||
+ | tbl2[k] = f(v, ...) |
||
+ | end |
||
+ | return tbl2 |
||
+ | end |
||
+ | |||
+ | function p.copy(tbl, f, ...) |
||
+ | local tbl2 = {} |
||
+ | for k, v in pairs(tbl) do |
||
tbl2[k] = f(v, ...) |
tbl2[k] = f(v, ...) |
||
end |
end |
||
Line 60: | Line 95: | ||
for _, v in ipairs(tbl) do |
for _, v in ipairs(tbl) do |
||
tbl[v] = f(tbl[v], ...) |
tbl[v] = f(tbl[v], ...) |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.selfDictInPlace(self, tbl, f, ...) |
||
+ | for _, v in ipairs(tbl) do |
||
+ | tbl[v] = f(self, tbl[v], ...) |
||
end |
end |
||
return tbl |
return tbl |
||
Line 68: | Line 110: | ||
f(row, ...) |
f(row, ...) |
||
row.index = k |
row.index = k |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.selfRowsInPlace(self, tbl, f, ...) |
||
+ | for k, row in ipairs(tbl) do |
||
+ | f(self, row, ...) |
||
+ | row.index = k |
||
+ | end |
||
+ | return tbl |
||
+ | end |
||
+ | |||
+ | function p.unorderedDictRowsInPlace(tbl, f, ...) |
||
+ | for k, row in pairs(tbl) do |
||
+ | f(row, ...) |
||
end |
end |
||
return tbl |
return tbl |
||
Line 98: | Line 155: | ||
local tbl = p.split(str, sep, f, ...) |
local tbl = p.split(str, sep, f, ...) |
||
return table.concat(tbl, sep2) |
return table.concat(tbl, sep2) |
||
+ | end |
||
+ | |||
+ | function p.multiParamSplitAndConcat(tbl, sep, f, sep2, ...) |
||
+ | if not tbl or not #tbl == 0 or not tbl[1] then return nil end |
||
+ | if not sep2 then sep2 = '' end |
||
+ | p.inPlace(tbl, p.split, sep) |
||
+ | local new = p.multiParamSafe(tbl, f, ...) |
||
+ | return table.concat(new, sep2) |
||
+ | end |
||
+ | |||
+ | function p.concatField(tbl, field, sep, f, ...) |
||
+ | return util_table.concat(p.extractField(tbl, field), sep, f, ...) |
||
end |
end |
||
Line 117: | Line 186: | ||
if not tbl then return nil end |
if not tbl then return nil end |
||
if not next(tbl) then return nil end |
if not next(tbl) then return nil end |
||
− | return util_table.concat(tbl, sep, |
+ | return util_table.concat(tbl, sep, p.formatInto, str) |
+ | end |
||
+ | |||
+ | function p.format(tbl, str) |
||
+ | if not tbl then return nil end |
||
+ | if not next(tbl) then return nil end |
||
+ | return p.safe(tbl, p.formatInto, str) |
||
+ | end |
||
+ | |||
+ | function p.formatInto(sub, str) |
||
+ | return str:format(sub) |
||
+ | end |
||
+ | |||
+ | function p.formatAndConcatTable(tbl, sep, sub) |
||
+ | if not tbl then return nil end |
||
+ | if not next(tbl) then return nil end |
||
+ | local tbl2 = p.formatTable(tbl, sub) |
||
+ | return util_table.concat(tbl2, sep) |
||
+ | end |
||
+ | |||
+ | function p.formatTable(tbl, sub) |
||
+ | if not tbl then return nil end |
||
+ | if not next(tbl) then return nil end |
||
+ | return p.safe(tbl, p.formatSub, sub) |
||
end |
end |
||
− | function |
+ | function p.formatSub(str, sub) |
return str:format(sub) |
return str:format(sub) |
||
end |
end |
Latest revision as of 02:13, 19 March 2022
Edit the documentation or categories for this module.
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local h = {}
local p = {}
function p.inPlace(tbl, f, ...)
for k, v in pairs(tbl) do
tbl[k] = f(v, ...)
end
return tbl
end
function p.apply(tbl, f, ...)
for k, v in pairs(tbl) do
f(v, ...)
end
return tbl
end
function p.arrayApply(tbl, f, ...)
for k, v in ipairs(tbl) do
f(v, ...)
end
return tbl
end
function p.multiParamSafe(tbl, f, ...)
if not tbl or #tbl == 0 or #tbl[1] == 0 then return end
local util_vars = require("Module:VarsUtil")
local new = util_table.interlace(tbl)
util_vars.log(tbl)
util_vars.log('********')
util_vars.log(new)
for i, v in ipairs(new) do
new[i] = f(unpack(new[i]))
end
return new
end
function p.arrayInPlace(tbl, f, ...)
for k, v in ipairs(tbl) do
tbl[k] = f(v, ...)
end
return tbl
end
function p.arrayInPlaceWithIndex(tbl, f, ...)
for k, v in ipairs(tbl) do
tbl[k] = f(v, k, ...)
end
return tbl
end
function p.arrayInPlaceAndMerge(tbl, f, ...)
-- assumes f returns table values
return util_table.mergeArrays(unpack(p.arrayInPlace(tbl, f, ...)))
end
function p.arraySafe(tbl, f, ...)
local tbl2 = {}
for k, v in ipairs(tbl) do
tbl2[k] = f(v, ...)
end
return tbl2
end
function p.safe(tbl, f, ...)
local tbl2 = mw.clone(tbl)
util_table.removeFalseEntries(tbl2)
for k, v in pairs(tbl2) do
tbl2[k] = f(v, ...)
end
return tbl2
end
function p.copy(tbl, f, ...)
local tbl2 = {}
for k, v in pairs(tbl) do
tbl2[k] = f(v, ...)
end
return tbl2
end
function p.dictSafe(tbl, f, ...)
local tbl2 = mw.clone(tbl)
for _, v in ipairs(tbl2) do
tbl2[v] = f(tbl2[v], ...)
end
return tbl2
end
function p.dictInPlace(tbl, f, ...)
for _, v in ipairs(tbl) do
tbl[v] = f(tbl[v], ...)
end
return tbl
end
function p.selfDictInPlace(self, tbl, f, ...)
for _, v in ipairs(tbl) do
tbl[v] = f(self, tbl[v], ...)
end
return tbl
end
function p.rowsInPlace(tbl, f, ...)
for k, row in ipairs(tbl) do
f(row, ...)
row.index = k
end
return tbl
end
function p.selfRowsInPlace(self, tbl, f, ...)
for k, row in ipairs(tbl) do
f(self, row, ...)
row.index = k
end
return tbl
end
function p.unorderedDictRowsInPlace(tbl, f, ...)
for k, row in pairs(tbl) do
f(row, ...)
end
return tbl
end
function p.dictRowsInPlace(tbl, f, ...)
for _, v in ipairs(tbl) do
f(tbl[v], ...)
end
return tbl
end
function p.arrayToLookupSafe(tbl, f, ...)
local tbl2 = {}
for _, v in ipairs(tbl) do
tbl2[v] = f(v, ...)
end
return tbl2
end
function p.split(str, sep, f, ...)
local tbl = util_text.split(str,sep)
if not f then return tbl end
return p.inPlace(tbl, f, ...)
end
function p.splitAndConcat(str, sep, f, sep2, ...)
if not str or str == '' then return nil end
if not sep2 then sep2 = '' end
local tbl = p.split(str, sep, f, ...)
return table.concat(tbl, sep2)
end
function p.multiParamSplitAndConcat(tbl, sep, f, sep2, ...)
if not tbl or not #tbl == 0 or not tbl[1] then return nil end
if not sep2 then sep2 = '' end
p.inPlace(tbl, p.split, sep)
local new = p.multiParamSafe(tbl, f, ...)
return table.concat(new, sep2)
end
function p.concatField(tbl, field, sep, f, ...)
return util_table.concat(p.extractField(tbl, field), sep, f, ...)
end
function p.extractField(tbl, field, f, ...)
local tbl2 = {}
if f then
for _, row in ipairs(tbl) do
tbl2[#tbl2+1] = f(row[field], ...)
end
else
for _, row in ipairs(tbl) do
tbl2[#tbl2+1] = row[field]
end
end
return tbl2
end
function p.formatAndConcat(tbl, sep, str)
if not tbl then return nil end
if not next(tbl) then return nil end
return util_table.concat(tbl, sep, p.formatInto, str)
end
function p.format(tbl, str)
if not tbl then return nil end
if not next(tbl) then return nil end
return p.safe(tbl, p.formatInto, str)
end
function p.formatInto(sub, str)
return str:format(sub)
end
function p.formatAndConcatTable(tbl, sep, sub)
if not tbl then return nil end
if not next(tbl) then return nil end
local tbl2 = p.formatTable(tbl, sub)
return util_table.concat(tbl2, sep)
end
function p.formatTable(tbl, sub)
if not tbl then return nil end
if not next(tbl) then return nil end
return p.safe(tbl, p.formatSub, sub)
end
function p.formatSub(str, sub)
return str:format(sub)
end
return p