Module:Array2DParser

-- Load modules local fun = require("Module:Functional") local iter = fun.iter

-- Module call from Template:Array2DParser local function encodeFromParserFunction( frame ) local args = require("Module:Args").getCleanArgs return table.concat(args, "|") end

-- Converts a row, which is a Lua table, into its string form, then appends it -- to the table that represents the array. Returns the updated array. local function appendRowAsString(arr, row) table.insert(arr, table.concat(row, ';')) return arr end

-- Encodes an 2D Lua array with numeric indices into a string. local function encodeFromLua(arr) return table.concat(iter(arr):reduce(appendRowAsString, {}), '|') end

-- Converts a row, which is a string, into its Lua table form, then appends it -- to the table that represents the array. Returns the updated array. local function appendRow(arr, str) table.insert(arr, mw.text.split(mw.text.trim(str), "%s*;%s*")) return arr end

-- Decodes a string into a 2D Lua array with numeric indices. local function decode(str) return iter(mw.text.gsplit(mw.text.trim(str), "%s*|%s*")):reduce(appendRow, {}) end

return { encodeFromParserFunction = encodeFromParserFunction, encodeFromLua = encodeFromLua, appendRow = appendRow, decode = decode, }