Module:Array: Difference between revisions

Jump to navigation Jump to search
osrsw>Talulah
m add take_from
osrsw>Talulah
m sync changes with rsw
Line 1: Line 1:
-- <nowiki> awawa
-- <nowiki> awawa
local libraryUtil = require('libraryUtil')
local p = {}
local p = {}


function p.any(enum, fn)
function p.any(enum, fn, clone)
libraryUtil.checkType('Module:Enum.any', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.any', 2, fn, 'function', true)
libraryUtil.checkType('Module:Enum.any', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
fn = fn or function(item) return item end
fn = fn or function(item) return item end
for _, item in ipairs(enum) do
for _, item in ipairs(enum) do
Line 12: Line 17:
end
end


function p.all(enum, fn)
function p.all(enum, fn, clone)
libraryUtil.checkType('Module:Enum.all', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.all', 2, fn, 'function', true)
libraryUtil.checkType('Module:Enum.all', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
fn = fn or function(item) return item end
fn = fn or function(item) return item end
for _, item in ipairs(enum) do
for _, item in ipairs(enum) do
Line 22: Line 31:
end
end


function p.each(enum, fn)
function p.each(enum, fn, clone)
libraryUtil.checkType('Module:Enum.each', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.each', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.each', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
for _, item in ipairs(enum) do
for _, item in ipairs(enum) do
fn(item)
fn(item)
Line 28: Line 41:
end
end


function p.filter(enum, fn)
function p.filter(enum, fn, clone)
libraryUtil.checkType('Module:Enum.filter', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.filter', 2, fn, 'function', true)
libraryUtil.checkType('Module:Enum.filter', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
fn = fn or function(item) return item end
fn = fn or function(item) return item end
local r = {}
local r = {}
Line 39: Line 56:
end
end


function p.find(enum, fn, default)
function p.find(enum, fn, default, clone)
libraryUtil.checkType('Module:Enum.find', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.find', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.find', 4, clone, 'boolean', true)
if clone then enum = mw.clone(enum); default = mw.clone(default) end
for _, item in ipairs(enum) do
for _, item in ipairs(enum) do
if fn(item) then
if fn(item) then
Line 48: Line 69:
end
end


function p.find_index(enum, fn, default)
function p.find_index(enum, fn, default, clone)
libraryUtil.checkType('Module:Enum.find_index', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.find_index', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.find_index', 4, clone, 'boolean', true)
if clone then enum = mw.clone(enum); default = mw.clone(default) end
for index, item in ipairs(enum) do
for index, item in ipairs(enum) do
if fn(item) then
if fn(item) then
Line 57: Line 82:
end
end


function p.map(enum, fn)
function p.map(enum, fn, clone)
libraryUtil.checkType('Module:Enum.map', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.map', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.map', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
local r = {}
local r = {}
for _, item in ipairs(enum) do
for _, item in ipairs(enum) do
table.insert(r, fn(item))
local temp = fn(item) -- Only use first returned item
table.insert(r, temp)
end
end
return r
return r
end
end


function p.max_by(enum, fn)
function p.max_by(enum, fn, clone)
return p.reduce(enum, function(new, old)
libraryUtil.checkType('Module:Enum.max_by', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.max_by', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.max_by', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
return unpack(p.reduce(enum, function(new, old)
local y = fn(new)
local y = fn(new)
return y > old[2] and {new, y} or old
return y > old[2] and {new, y} or old
end, {0, 0})
end, {0, 0}))
end
end


function p.reduce(enum, fn, accumulator)
function p.reduce(enum, fn, accumulator, clone)
libraryUtil.checkType('Module:Enum.reduce', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.reduce', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.reduce', 4, clone, 'boolean', true)
if clone then enum = mw.clone(enum); accumulator = mw.clone(accumulator) end
local acc = accumulator
local acc = accumulator
for index, item in ipairs(enum) do
for index, item in ipairs(enum) do
Line 84: Line 122:
end
end


function p.reject(enum, fn)
function p.reject(enum, fn, clone)
libraryUtil.checkType('Module:Enum.reject', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.reject', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.reject', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
fn = fn or function(item) return item end
local r = {}
local r = {}
for index, item in ipairs(enum) do
for _, item in ipairs(enum) do
if not fn(item, index) then
if not fn(item) then
table.insert(r, item)
table.insert(r, item)
end
end
Line 94: Line 137:
end
end


function p.scan(enum, fn, accumulator)
function p.range(start, stop, step)
libraryUtil.checkType('Module:Enum.range', 1, start, 'number')
libraryUtil.checkType('Module:Enum.range', 2, stop, 'number', true)
libraryUtil.checkType('Module:Enum.range', 3, step, 'number', true)
local array = {}
if not stop then
stop = start
start = 1
end
for i = start, stop, step or 1 do
table.insert(array, i)
end
return array
end
 
function p.scan(enum, fn, accumulator, clone)
libraryUtil.checkType('Module:Enum.scan', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.scan', 2, fn, 'function')
libraryUtil.checkType('Module:Enum.scan', 4, clone, 'boolean', true)
if clone then enum = mw.clone(enum); accumulator = mw.clone(accumulator) end
local acc = accumulator
local acc = accumulator
local r = {}
local r = {}
Line 108: Line 170:
end
end


function p.slice(enum, start, finish)
function p.slice(enum, start, finish, clone)
libraryUtil.checkType('Module:Enum.slice', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.slice', 2, start, 'number', true)
libraryUtil.checkType('Module:Enum.slice', 3, finish, 'number', true)
libraryUtil.checkType('Module:Enum.slice', 4, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
start = start or 1
finish = finish or #enum
local r = {}
local r = {}
for index, item in ipairs(enum) do
for index, item in ipairs(enum) do
Line 118: Line 187:
end
end


function p.split(enum, count)
function p.split(enum, count, clone)
libraryUtil.checkType('Module:Enum.split', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.split', 2, count, 'number')
libraryUtil.checkType('Module:Enum.split', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
if #enum < count then
if #enum < count then
return enum, {}
return enum, {}
elseif count < 1 then
return {}, enum
end
end


Line 135: Line 210:
end
end


function p.sum(enum)
function p.sum(enum, clone)
libraryUtil.checkType('Module:Enum.sum', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.sum', 2, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
return p.reduce(enum, function(x, y) return x + y end)
return p.reduce(enum, function(x, y) return x + y end)
end
end


function p.take(enum, count)
function p.take(enum, count, clone)
libraryUtil.checkType('Module:Enum.take', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.take', 2, count, 'number')
libraryUtil.checkType('Module:Enum.take', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
local x, _ = p.split(enum, count)
local x, _ = p.split(enum, count)
return x
return x
end
end


function p.take_every(enum, n)
function p.take_every(enum, n, clone)
libraryUtil.checkType('Module:Enum.take_every', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.take_every', 2, n, 'number')
libraryUtil.checkType('Module:Enum.take_every', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
local r = {}
local r = {}
for index, item in ipairs(enum) do
for index, item in ipairs(enum) do
Line 154: Line 240:
end
end


function p.unique(enum)
function p.unique(enum, fn, clone)
libraryUtil.checkType('Module:Enum.unique', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.unique', 2, fn, 'function', true)
libraryUtil.checkType('Module:Enum.unique', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum) end
fn = fn or function(item) return item end
local r = {}
local r = {}
local hash = {}
local hash = {}
for _, item in ipairs(enum) do
for _, item in ipairs(enum) do
if not hash[item] then
local id = fn(item)
if not hash[id] then
table.insert(r, item)
table.insert(r, item)
hash[item] = true
hash[id] = true
end
end
end
end
Line 166: Line 258:
end
end


function p.zip(enums)
function p.zip(enums, clone)
libraryUtil.checkType('Module:Enum.zip', 1, enums, 'table')
libraryUtil.checkType('Module:Enum.zip', 2, clone, 'boolean', true)
if clone then enums = mw.clone(enums) end
local r = {}
local r = {}
local longest = p.max_by(enums, function(enum) return #enum end)
local _, longest = p.max_by(enums, function(enum) return #enum end)
for i = 1, longest[2] do
for i = 1, longest do
local q = {}
local q = {}
for j = 1, #enums do
for j = 1, #enums do
Line 186: Line 281:
), count)
), count)
return x
return x
end
function p.intersect(enum1, enum2, clone)
libraryUtil.checkType('Module:Enum.intersect', 1, enum1, 'table')
libraryUtil.checkType('Module:Enum.intersect', 2, enum2, 'table')
libraryUtil.checkType('Module:Enum.intersect', 3, clone, 'boolean', true)
if clone then enum1 = mw.clone(enum1); enum2 = mw.clone(enum2) end
return p.any(enum1, function(item1) return p.any(enum2, function(item2) return item1==item2 end) end)
end
function p.contains(enum, elem, clone)
libraryUtil.checkType('Module:Enum.contains', 1, enum, 'table')
libraryUtil.checkType('Module:Enum.contains', 3, clone, 'boolean', true)
if clone then enum = mw.clone(enum); elem = mw.clone(elem) end
return p.any(enum, function(item) return item == elem end)
end
end


return p
return p
-- </nowiki>
-- </nowiki>