• Welcome to the Kancolle Wiki!
  • If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord

Difference between revisions of "Module:Core"

From Kancolle Wiki
Jump to navigation Jump to search
m
Line 25: Line 25:
 
function Utils.map(tbl, fn)
 
function Utils.map(tbl, fn)
 
local result = {}
 
local result = {}
for i, v in pairs(tbl) do
+
for _, v in pairs(tbl) do
result[i] = fn(v)
+
table.insert(result, fn(v))
 
end
 
end
 
return result
 
return result
 
end
 
end
 
+
 
function Utils.filter(tbl, pred)
 
function Utils.filter(tbl, pred)
 
local result = {}
 
local result = {}
for i, v in pairs(tbl) do
+
for _, v in pairs(tbl) do
 
if pred(v) then
 
if pred(v) then
result[i] = v
+
table.insert(result, v)
 
end
 
end
 
end
 
end

Revision as of 07:40, 15 January 2016

Documentation for this module may be created at Module:Core/doc

local Utils = {}

function Utils.method(frame)
	local m = require("Module:" .. frame.args[1])
	local f = frame.args[2]
	local args = {}
	for k, v in pairs(frame.args) do
		if type(k) == "number" and k >= 3 and type(v) == "string" then
			table.insert(args, v)
		end
	end
	return m[f](m, unpack(args))
end

function Utils.find(tbl, v_, k_)
	for _, v in pairs(tbl) do
		if k_ and v[k_] == v_ or not k_ and v == v_ then
			return true
		end
	end
	return false
end

function Utils.map(tbl, fn)
	local result = {}
	for _, v in pairs(tbl) do
		table.insert(result, fn(v))
	end
	return result
end
 
function Utils.filter(tbl, pred)
	local result = {}
	for _, v in pairs(tbl) do
		if pred(v) then
			table.insert(result, v)
		end
	end
	return result
end

return Utils