goal:
The function below takes in two tables, and applies an operation to the first table. For example:
local tbl1 = {{5}, {6}, {7}}
local tbl1Size = 2 --because it is a 2 dimensional table/matrix
local tbl2 = {3,3,3}
local tbl2Size = 1 --because it is a 1 dimensional table
local function mul(a, b)
return a * b
end
print(choppedOp(tbl1, tbl1Size, tbl2, tbl2Size, mul, 1))
--[[output:
{{15}, {18}, {21}}
]]
(see image at bottom for drawn out representation)
issue:
If the first table's depth (goalDimensions) is >= the otherData's depth (otherDimensions) this function works fine. The issue arises when otherDimensions is greater than goalDimensions, then the function will error.
code:
local function choppedOp(goalData: {}, goalDimensions: number, otherData: {}, otherDimensions: number, op: operation, dimension: number)
if type(goalData) == "table" then
if goalDimensions - otherDimensions - (dimension - 1) > 0 then
for i = 1, #goalData do
goalData[i] = choppedOp(goalData[i], goalDimensions, otherData, otherDimensions, op, dimension + 1)
end
else
for i = 1, #goalData do
goalData[i] = choppedOp(goalData[i], goalDimensions, (otherData[i] ~= nil) and otherData[i] or otherData[1], otherDimensions, op, dimension + 1)
end
end
else
goalData = op(goalData, otherData)
end
return goalData
end