Module:Array: Difference between revisionsGive feedback
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, | 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 | for _, item in ipairs(enum) do | ||
if not fn(item | 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[ | local id = fn(item) | ||
if not hash[id] then | |||
table.insert(r, item) | table.insert(r, item) | ||
hash[ | 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 | 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> | ||
Revision as of 00:12, 18 February 2020
Documentation for this module may be created at Module:Array/doc
-- <nowiki> awawa
local libraryUtil = require('libraryUtil')
local p = {}
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
for _, item in ipairs(enum) do
if fn(item) then
return true
end
end
return false
end
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
for _, item in ipairs(enum) do
if not fn(item) then
return false
end
end
return true
end
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
fn(item)
end
end
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
local r = {}
for _, item in ipairs(enum) do
if fn(item) then
table.insert(r, item)
end
end
return r
end
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
if fn(item) then
return item
end
end
return default
end
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
if fn(item) then
return index
end
end
return default
end
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 = {}
for _, item in ipairs(enum) do
local temp = fn(item) -- Only use first returned item
table.insert(r, temp)
end
return r
end
function p.max_by(enum, fn, clone)
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)
return y > old[2] and {new, y} or old
end, {0, 0}))
end
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
for index, item in ipairs(enum) do
if index == 1 and not accumulator then
acc = item
else
acc = fn(item, acc)
end
end
return acc
end
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 = {}
for _, item in ipairs(enum) do
if not fn(item) then
table.insert(r, item)
end
end
return r
end
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 r = {}
for index, item in ipairs(enum) do
if index == 1 and not accumulator then
acc = item
else
acc = fn(item, acc)
end
table.insert(r, acc)
end
return r
end
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 = {}
for index, item in ipairs(enum) do
if index >= start and index <= finish then
table.insert(r, item)
end
end
return r
end
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
return enum, {}
elseif count < 1 then
return {}, enum
end
local x = {}
local y = {}
for i = 1, #enum do
table.insert(
i <= count and x or y,
enum[i]
)
end
return x, y
end
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)
end
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)
return x
end
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 = {}
for index, item in ipairs(enum) do
if (index - 1) % n == 0 then
table.insert(r, item)
end
end
return r
end
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 hash = {}
for _, item in ipairs(enum) do
local id = fn(item)
if not hash[id] then
table.insert(r, item)
hash[id] = true
end
end
return r
end
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 _, longest = p.max_by(enums, function(enum) return #enum end)
for i = 1, longest do
local q = {}
for j = 1, #enums do
table.insert(q, enums[j][i])
end
table.insert(r, q)
end
return r
end
function p.take_from(enum, index, count)
local x, _ = p.split(p.reject(enum,
function(item, idx)
return idx < index
end
), count)
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
return p
-- </nowiki>