Module:Enum
Lua error in Module:DependencyList at line 212: attempt to call field 'insert' (a nil value).
This module is a helper module to be used by other modules; it may not designed to be invoked directly. See GSWiki:Lua/Helper modules for a full list and more information. For a full list of modules using this helper click here
| Module | Function | Type | Use | Example |
|---|---|---|---|---|
| Enum | all( enum, [fn], [clone|false] ) | table, function/nil, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. If fn returns true for all elements then all() returns true, otherwise false. If no function is given function( item ) return item end is used. | |
any( enum, [fn], [clone|false] ) | table, function/nil, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. If fn returns true for at least one element then any() returns true, otherwise false. If no function is given function( item ) return item end is used. If clone is true the input enum is deep copied using mw.clone() before use. | ||
contains( enum, elem, [clone|false] ) | table, any, boolean/nil | Returns true if enum contains elem, otherwise returns false. | ||
each( enum, fn, [clone|false] ) | table, function, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. This function returns nothing. | ||
filter( enum, [fn], [clone|false] ) | table, function/nil, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. If fn returns true the corresponding element is copied to a new table which is then returned. If no function is given function( item ) return item end is used. | ||
find( enum, fn, [default|nil], [clone|false] ) | table, function, any, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. The first element where fn returns true is returned. If no elements passes the test, default is returned. | ||
find_index( enum, fn, [default|nil], [clone|false] ) | table, function, any, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. The index of the first element where fn returns true is returned. If no elements passes the test, default is returned. | ||
insert( enum1, enum2, [index|#enum1+1], [clone|false] ) | table, table, number, boolean/nil | Returns a new table with enum2 inserted into enum1 at index index. If no index is given, enum2 is appended to enum1. | ||
intersect( enum1, enum2, [clone|false] ) | table, table, boolean/nil | Returns a table containing elements which exist in both enums. The order of the elements is the same as the order they occure in enum1. | ||
intersects( enum1, enum2, [clone|false] ) | table, table, boolean/nil | Returns true if both sets have at least one element with equal values. | ||
map( enum, fn, [clone|false] ) | table, function, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. The return value of fn is appended to a new table. This new table is returned. | ||
max_by( enum, fn, [clone|false] ) | table, function, boolean/nil | Loops over the array part of enum and passes each element as the first argument to fn. max_by() returns two values, the first is the element where fn returned the largest value, the second is this largest value. The > operator is used for the comparisons. | ||
new( [enum|{}] ) | table | Adds a metatable to enum which allows it use elementwise math operations on the table. Also makes it possible to use the colon : operator with all the other functions listed here that take a table as their first argument.
local t = enum.new{1, 2, 3}
local t2 = enum{4, 5, 6} -- Alternative notation
mw.log( -t ) --> { -1, -2, -3 }
mw.log( t + 2 ) --> { 3, 4, 5 }
mw.log( t - 2 ) --> { -1, 0, 1 }
mw.log( t * 2 ) --> { 2, 4, 6 }
mw.log( t / 2 ) --> { 0.5, 1, 1.5 }
mw.log( t ^ 2 ) --> { 1, 4, 9 }
mw.log( t + t2 ) --> { 5, 7, 9 }
mw.log( t .. t2 ) --> { 1, 2, 3, 4, 5, 6 }
mw.log( t:sum() ) --> 6
mw.log( (t .. t2):reject{3, 4, 5} ) --> { 1, 2, 6 }
| ||
newIncrementor( [start|1], [step|1] ) | number, number | Returns a new incrementor function. Every time this incrementor function is called it returns a number step higher than the previous call. The current value can be obtained with inc.n or set inc.n = number where inc is an incrementor function. The step size can be changed with inc.step = number. | ||
range( stop )range( start, stop, [step|1] ) | number, number, number | Returns a table containing a sequence of numbers from start to stop (both inclusive if ints, end-exclusive if floats) by step. range(4) produces {1, 2, 3, 4} (start defaults to 1). range(0, 4) produces {0, 1, 2, 3, 4}. When step is given, it specifies the increment (or decrement). | ||
reduce( enum, fn, [accumulator|enum[1]], [clone|false] ) | table, function, any, boolean/nil | Loops over the array part of enum and passes each element as the first argument and accumulator as the second to fn. The return value of fn becomes the new accumulator. The final value of accumulator is returned. If no accumulator is given then the first element in enum is used. | ||
reject( enum, [fn], [clone|false] )reject( enum, [table], [clone|false] ) | table, function/table, boolean/nil | The opposite of filter(). If the second argument is a table, every element in enum equal to any of the values in this table will be rejected. | ||
rep( val, n, [clone|false] ) | any, number, boolean | Returns a table with n copies of val. | ||
scan( enum, fn, [accumulator|enum[1]], [clone|false] ) | table, function, any, boolean/nil | Same as reduce() but each step is appended to a table. This table is then returned. | ||
slice( enum, [start|1], [stop|#enum], [clone|false] ) | table, number/nil, number/nil, boolean/nil | Returns a table containing all the elements of enum between the start and stop indices. The start and stop indices are inclusive. | ||
split( enum, count, [clone|false] ) | table, number, boolean/nil | Returns two tables where the first is equivalent to slice( enum, 1, count ) and the second slice( enum, count+1, #enum ). | ||
sum( enum, [clone|false] ) | table, boolean/nil | Returns the sum of all array elements in enum. | ||
take( enum, count, [clone|false] ) | table, number, boolean/nil | Returns only the first table of split( enum, count ). | ||
take_every( enum, n, [clone|false] ) | table, number, boolean/nil | Returns a table containing every nth element of enum. | ||
unique( enum, [fn], [clone|false] ) | table, function/nil, boolean/nil | Returns a new table where all duplicate values in enum are removed. In case a duplicate is present, the element with the lowest index will be kept and every subsequent duplicate element is removed. fn can be used to create a custom id for every element so that the result is a table with elements which all create a unique id. If no function is given function( item ) return item end is used. | ||
zip( enums, [clone|false] ) | table, boolean/nil | Groups elements with the same indexes from different arrays together, i.e. zip{ {a1, a2, a3}, {b1, b2, b3}, {c1, c2} } -> {{a1, b1, c1}, {a2, b2, c2}, {a3, b3}} |
-- <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.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.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>