Module:Array: Difference between revisions

osrsw>CephHunter
m CephHunter moved Module:Enum to Module:Array without leaving a redirect: More fitting name and to match name on rsw
osrsw>CephHunter
No edit summary
Line 52: Line 52:


local function mathTemplate(lhs, rhs, funName, fun)
local function mathTemplate(lhs, rhs, funName, fun)
checkTypeMulti('Module:Enum.' .. funName, 1, lhs, {'number', 'table'})
checkTypeMulti('Module:Array.' .. funName, 1, lhs, {'number', 'table'})
checkTypeMulti('Module:Enum.' .. funName, 2, rhs, {'number', 'table'})
checkTypeMulti('Module:Array.' .. funName, 2, rhs, {'number', 'table'})
local res = setmetatable({}, getmetatable(lhs) or getmetatable(rhs))
local res = setmetatable({}, getmetatable(lhs) or getmetatable(rhs))


Line 125: Line 125:


function arr.all(array, fn)
function arr.all(array, fn)
checkType('Module:Enum.all', 1, array, 'table')
checkType('Module:Array.all', 1, array, 'table')
if fn == nil then fn = function(item) return item end end
if fn == nil then fn = function(item) return item end end
if type(fn) ~= 'function' then
if type(fn) ~= 'function' then
Line 142: Line 142:


function arr.any(array, fn)
function arr.any(array, fn)
checkType('Module:Enum.any', 1, array, 'table')
checkType('Module:Array.any', 1, array, 'table')
if fn == nil then fn = function(item) return item end end
if fn == nil then fn = function(item) return item end end
if type(fn) ~= 'function' then
if type(fn) ~= 'function' then
Line 159: Line 159:


function arr.clean(array)
function arr.clean(array)
checkType('Module:Enum.clean', 1, array, 'table')
checkType('Module:Array.clean', 1, array, 'table')
for i = 1, #array do
for i = 1, #array do
if type(array[i]) == 'table' then
if type(array[i]) == 'table' then
Line 170: Line 170:


function arr.contains(array, elem, useElemTableContent)
function arr.contains(array, elem, useElemTableContent)
checkType('Module:Enum.contains', 1, array, 'table')
checkType('Module:Array.contains', 1, array, 'table')
if type(elem) == 'table' and useElemTableContent ~= false then
if type(elem) == 'table' and useElemTableContent ~= false then
local elemMap = {}
local elemMap = {}
Line 188: Line 188:


function arr.count(array, fn)
function arr.count(array, fn)
checkType('Module:Enum.count', 1, array, 'table')
checkType('Module:Array.count', 1, array, 'table')
if fn == nil then fn = function(item) return item end end
if fn == nil then fn = function(item) return item end end
if type(fn) ~= 'function' then
if type(fn) ~= 'function' then
Line 204: Line 204:


function arr.diff(array, order)
function arr.diff(array, order)
checkType('Module:Enum.diff', 1, array, 'table')
checkType('Module:Array.diff', 1, array, 'table')
checkType('Module:Enum.diff', 2, order, 'number', true)
checkType('Module:Array.diff', 2, order, 'number', true)
local res = setmetatable({}, getmetatable(array))
local res = setmetatable({}, getmetatable(array))
for i = 1, #array - 1 do
for i = 1, #array - 1 do
Line 217: Line 217:


function arr.each(array, fn)
function arr.each(array, fn)
checkType('Module:Enum.each', 1, array, 'table')
checkType('Module:Array.each', 1, array, 'table')
checkType('Module:Enum.each', 2, fn, 'function')
checkType('Module:Array.each', 2, fn, 'function')
local i = 1
local i = 1
while array[i] ~= nil do
while array[i] ~= nil do
Line 227: Line 227:


function arr.filter(array, fn)
function arr.filter(array, fn)
checkType('Module:Enum.filter', 1, array, 'table')
checkType('Module:Array.filter', 1, array, 'table')
if fn == nil then fn = function(item) return item end end
if fn == nil then fn = function(item) return item end end
if type(fn) ~= 'function' then
if type(fn) ~= 'function' then
Line 247: Line 247:


function arr.find(array, fn, default)
function arr.find(array, fn, default)
checkType('Module:Enum.find', 1, array, 'table')
checkType('Module:Array.find', 1, array, 'table')
checkTypeMulti('Module:Enum.find_index', 2, fn, {'function', 'table', 'number', 'boolean'})
checkTypeMulti('Module:Array.find_index', 2, fn, {'function', 'table', 'number', 'boolean'})
if type(fn) ~= 'function' then
if type(fn) ~= 'function' then
local val = fn
local val = fn
Line 264: Line 264:


function arr.find_index(array, fn, default)
function arr.find_index(array, fn, default)
checkType('Module:Enum.find_index', 1, array, 'table')
checkType('Module:Array.find_index', 1, array, 'table')
checkTypeMulti('Module:Enum.find_index', 2, fn, {'function', 'table', 'number', 'boolean'})
checkTypeMulti('Module:Array.find_index', 2, fn, {'function', 'table', 'number', 'boolean'})
if type(fn) ~= 'function' then
if type(fn) ~= 'function' then
local val = fn
local val = fn
Line 281: Line 281:


function arr.newIncrementor(start, step)
function arr.newIncrementor(start, step)
checkType('Module:Enum.newIncrementor', 1, start, 'number', true)
checkType('Module:Array.newIncrementor', 1, start, 'number', true)
checkType('Module:Enum.newIncrementor', 2, step, 'number', true)
checkType('Module:Array.newIncrementor', 2, step, 'number', true)
step = step or 1
step = step or 1
local n = (start or 1) - step
local n = (start or 1) - step
Line 302: Line 302:


function arr.int(array, start, stop)
function arr.int(array, start, stop)
checkType('Module:Enum.int', 1, array, 'table')
checkType('Module:Array.int', 1, array, 'table')
checkType('Module:Enum.int', 2, start, 'number', true)
checkType('Module:Array.int', 2, start, 'number', true)
checkType('Module:Enum.int', 3, stop, 'number', true)
checkType('Module:Array.int', 3, stop, 'number', true)
local res = setmetatable({}, getmetatable(array))
local res = setmetatable({}, getmetatable(array))
start = start or 1
start = start or 1
Line 316: Line 316:


function arr.intersect(array1, array2)
function arr.intersect(array1, array2)
checkType('Module:Enum.intersect', 1, array1, 'table')
checkType('Module:Array.intersect', 1, array1, 'table')
checkType('Module:Enum.intersect', 2, array2, 'table')
checkType('Module:Array.intersect', 2, array2, 'table')
local array2Elements = {}
local array2Elements = {}
local res = setmetatable({}, getmetatable(array1) or getmetatable(array2))
local res = setmetatable({}, getmetatable(array1) or getmetatable(array2))
Line 332: Line 332:


function arr.intersects(array1, array2)
function arr.intersects(array1, array2)
checkType('Module:Enum.intersects', 1, array1, 'table')
checkType('Module:Array.intersects', 1, array1, 'table')
checkType('Module:Enum.intersects', 2, array2, 'table')
checkType('Module:Array.intersects', 2, array2, 'table')
local small = {}
local small = {}
local large
local large
Line 347: Line 347:


function arr.insert(array, val, index, unpackVal)
function arr.insert(array, val, index, unpackVal)
checkType('Module:Enum.insert', 1, array, 'table')
checkType('Module:Array.insert', 1, array, 'table')
checkType('Module:Enum.insert', 3, index, 'number', true)
checkType('Module:Array.insert', 3, index, 'number', true)
checkType('Module:Enum.insert', 4, unpackVal, 'boolean', true)
checkType('Module:Array.insert', 4, unpackVal, 'boolean', true)
local len = #array
local len = #array
index = index or (len + 1)
index = index or (len + 1)
Line 369: Line 369:


function arr.map(array, fn)
function arr.map(array, fn)
checkType('Module:Enum.map', 1, array, 'table')
checkType('Module:Array.map', 1, array, 'table')
checkType('Module:Enum.map', 2, fn, 'function')
checkType('Module:Array.map', 2, fn, 'function')
local len = 0
local len = 0
local r = setmetatable({}, getmetatable(array))
local r = setmetatable({}, getmetatable(array))
Line 386: Line 386:


function arr.max_by(array, fn)
function arr.max_by(array, fn)
checkType('Module:Enum.max_by', 1, array, 'table')
checkType('Module:Array.max_by', 1, array, 'table')
checkType('Module:Enum.max_by', 2, fn, 'function')
checkType('Module:Array.max_by', 2, fn, 'function')
return unpack(arr.reduce(array, function(new, old, i)
return unpack(arr.reduce(array, function(new, old, i)
local y = fn(new)
local y = fn(new)
Line 395: Line 395:


function arr.max(array)
function arr.max(array)
checkType('Module:Enum.max', 1, array, 'table')
checkType('Module:Array.max', 1, array, 'table')
local val, _, i = arr.max_by(array, function(x) return x end)
local val, _, i = arr.max_by(array, function(x) return x end)
return val, i
return val, i
Line 401: Line 401:


function arr.min(array)
function arr.min(array)
checkType('Module:Enum.min', 1, array, 'table')
checkType('Module:Array.min', 1, array, 'table')
local val, _, i = arr.max_by(array, function(x) return -x end)
local val, _, i = arr.max_by(array, function(x) return -x end)
return val, i
return val, i
Line 422: Line 422:


function arr.range(start, stop, step)
function arr.range(start, stop, step)
checkType('Module:Enum.range', 1, start, 'number')
checkType('Module:Array.range', 1, start, 'number')
checkType('Module:Enum.range', 2, stop, 'number', true)
checkType('Module:Array.range', 2, stop, 'number', true)
checkType('Module:Enum.range', 3, step, 'number', true)
checkType('Module:Array.range', 3, step, 'number', true)
local array = setmetatable({}, arr)
local array = setmetatable({}, arr)
local len = 0
local len = 0
Line 439: Line 439:


function arr.reduce(array, fn, accumulator)
function arr.reduce(array, fn, accumulator)
checkType('Module:Enum.reduce', 1, array, 'table')
checkType('Module:Array.reduce', 1, array, 'table')
checkType('Module:Enum.reduce', 2, fn, 'function')
checkType('Module:Array.reduce', 2, fn, 'function')
local acc = accumulator
local acc = accumulator
local i = 1
local i = 1
Line 455: Line 455:


function arr.reject(array, fn)
function arr.reject(array, fn)
checkType('Module:Enum.reject', 1, array, 'table')
checkType('Module:Array.reject', 1, array, 'table')
checkTypeMulti('Module:Enum.reject', 2, fn, {'function', 'table', 'number', 'boolean'})
checkTypeMulti('Module:Array.reject', 2, fn, {'function', 'table', 'number', 'boolean'})
if fn == nil then fn = function(item) return item end end
if fn == nil then fn = function(item) return item end end
if type(fn) ~= 'function' and type(fn) ~= 'table' then
if type(fn) ~= 'function' and type(fn) ~= 'table' then
Line 488: Line 488:


function arr.rep(val, n)
function arr.rep(val, n)
checkType('Module:Enum.rep', 2, n, 'number')
checkType('Module:Array.rep', 2, n, 'number')
local r = setmetatable({}, arr)
local r = setmetatable({}, arr)
for i = 1, n do
for i = 1, n do
Line 497: Line 497:


function arr.scan(array, fn, accumulator)
function arr.scan(array, fn, accumulator)
checkType('Module:Enum.scan', 1, array, 'table')
checkType('Module:Array.scan', 1, array, 'table')
checkType('Module:Enum.scan', 2, fn, 'function')
checkType('Module:Array.scan', 2, fn, 'function')
local acc = accumulator
local acc = accumulator
local r = setmetatable({}, getmetatable(array))
local r = setmetatable({}, getmetatable(array))
Line 515: Line 515:


function arr.slice(array, start, finish)
function arr.slice(array, start, finish)
checkType('Module:Enum.slice', 1, array, 'table')
checkType('Module:Array.slice', 1, array, 'table')
checkType('Module:Enum.slice', 2, start, 'number', true)
checkType('Module:Array.slice', 2, start, 'number', true)
checkType('Module:Enum.slice', 3, finish, 'number', true)
checkType('Module:Array.slice', 3, finish, 'number', true)
start = start or 1
start = start or 1
finish = finish or #array
finish = finish or #array
Line 539: Line 539:


function arr.split(array, count)
function arr.split(array, count)
checkType('Module:Enum.split', 1, array, 'table')
checkType('Module:Array.split', 1, array, 'table')
checkType('Module:Enum.split', 2, count, 'number')
checkType('Module:Array.split', 2, count, 'number')
local x = setmetatable({}, getmetatable(array))
local x = setmetatable({}, getmetatable(array))
local y = setmetatable({}, getmetatable(array))
local y = setmetatable({}, getmetatable(array))
Line 550: Line 550:


function arr.sum(array)
function arr.sum(array)
checkType('Module:Enum.sum', 1, array, 'table')
checkType('Module:Array.sum', 1, array, 'table')
local res = 0
local res = 0
for i = 1, #array do
for i = 1, #array do
Line 559: Line 559:


function arr.take(array, count, offset)
function arr.take(array, count, offset)
checkType('Module:Enum.take', 1, array, 'table')
checkType('Module:Array.take', 1, array, 'table')
checkType('Module:Enum.take', 2, count, 'number')
checkType('Module:Array.take', 2, count, 'number')
checkType('Module:Enum.take', 3, offset, 'number', true)
checkType('Module:Array.take', 3, offset, 'number', true)
local x = setmetatable({}, getmetatable(array))
local x = setmetatable({}, getmetatable(array))
for i = offset or 1, #array do
for i = offset or 1, #array do
Line 572: Line 572:


function arr.take_every(array, n, offset)
function arr.take_every(array, n, offset)
checkType('Module:Enum.take_every', 1, array, 'table')
checkType('Module:Array.take_every', 1, array, 'table')
checkType('Module:Enum.take_every', 2, n, 'number')
checkType('Module:Array.take_every', 2, n, 'number')
checkType('Module:Enum.take_every', 3, offset, 'number', true)
checkType('Module:Array.take_every', 3, offset, 'number', true)
local r = setmetatable({}, getmetatable(array))
local r = setmetatable({}, getmetatable(array))
local len = 0
local len = 0
Line 587: Line 587:


function arr.unique(array, fn)
function arr.unique(array, fn)
checkType('Module:Enum.unique', 1, array, 'table')
checkType('Module:Array.unique', 1, array, 'table')
checkType('Module:Enum.unique', 2, fn, 'function', true)
checkType('Module:Array.unique', 2, fn, 'function', true)
fn = fn or function(item) return item end
fn = fn or function(item) return item end
local r = setmetatable({}, getmetatable(array))
local r = setmetatable({}, getmetatable(array))
Line 607: Line 607:


function arr.update(array, indexes, values)
function arr.update(array, indexes, values)
checkType('Module:Enum.update', 1, array, 'table')
checkType('Module:Array.update', 1, array, 'table')
checkTypeMulti('Module:Enum.update', 2, indexes, {'table', 'number'})
checkTypeMulti('Module:Array.update', 2, indexes, {'table', 'number'})
if type(indexes) == 'number' then
if type(indexes) == 'number' then
indexes = {indexes}
indexes = {indexes}
Line 627: Line 627:
function arr.zip(...)
function arr.zip(...)
local arrays = { ... }
local arrays = { ... }
checkType('Module:Enum.zip', 1, arrays[1], 'table')
checkType('Module:Array.zip', 1, arrays[1], 'table')
local r = setmetatable({}, getmetatable(arrays[1]))
local r = setmetatable({}, getmetatable(arrays[1]))
local _, longest = arr.max_by(arrays, function(array) return #array end)
local _, longest = arr.max_by(arrays, function(array) return #array end)