Fire Emblem Heroes Wiki
Advertisement
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Template-info Documentation

Test cases

Lua metamodule containing higher-order functions for non-sequence tables.
  • t: Table which contains arbitrary key-value pairs.
  • f: Function which accepts a value from a table as its first argument and the value's corresponding key as its second argument.
  • pr: Predicate which accepts a value from a table as its first argument and the value's corresponding key as its second argument. Only truthiness of the return value is considered.
  • op: Function which accepts two table values.

All functions on tables use pairs to access their elements and therefore support the __pairs metamethod.

Generation

  • from_lists (ks, vs)
Creates a table from a list of keys and a list of values. Later duplicate keys take precedence.
  • from_ipairs (s, g)
Creates a table by calling g on each value-key pair of the sequence s; the first two value returned from g are then used to form key-value pairs in the result table. Nil keys are ignored.
  • from_pairs (t, g)
Creates a table by calling g on each value-key pair of an arbitrary table t; the first two value returned from g are then used to form key-value pairs in the result table. Nil keys are ignored.
  • generate (s, g)
Returns {[s[1]] = g(s[1], 1), [s[2]] = g(s[2], 2), ..., [s[#s]] = g(s[#s], #s)}.

Counting

  • size (t)
Returns the number of key-value pairs in t.
  • any (t, pr)
Returns true if pr is true for any element of t.
  • all (t, pr)
Returns true if pr is true for all elements of t.
  • none (t, pr)
Returns true if pr is true for none of the elements of t.
  • count (t, x)
Returns the number of values of t that compare equal to x.
  • count_if (t, pr)
Returns the number of elements of t that satisfy pr.

Searching

  • find (t, x)
Returns the key-value pair of any of the table's elements whose value compares equal to x, or nil if none can be found.
  • find_if (t, pr)
Returns the key-value pair of any of the table's elements that satisfies pr, or nil if none can be found.

Transformation

  • clone (t)
Returns a shallow copy of t.
  • map (t, f)
Returns a new table where each value is replaced with the result of calling f on that value-key pair. Keys are unchanged.
  • map_self (t, f)
Replaces every value of t with the result of calling f on that value-key pair. Returns t.
  • zip (t1, t2, op)
Returns a new table where the value for each key in t1 is the result of calling op on that value and the corresponding value in t2 with the same key (might be nil).
  • select (t, pr)
Returns a new table containing only the elements of t that satisfy pr.
  • merge (t1, t2, g)
Returns a new table containing key-value pairs from both tables. g accepts the values from both tables plus their common key, and is called if the same key exists in both tables; the return value of g is used to produce the value in the returned table. If not given, values in t2 take precedence, as if by function (v1, v2, k) return v2 end.
  • merge_self (t1, t2, g)
Adds all key-value pairs from t2 to t1. Returns t1. g has the same meaning as above.
  • invert (t)
Returns a new table with keys becoming values and values becoming keys. Duplicate keys are overwritten in an arbitrary order.
  • keys (t)
Returns a sequence containing the keys of the table in an arbitrary order.
  • values (t)
Returns a sequence containing the values of the table in an arbitrary order.
  • to_lists (t)
Returns keys(t), values(t). The orders from the two sequences are guaranteed to be the same.
  • transpose (t)
Given a table-of-tables t, returns a new table z with row indices and column indices swapped such that z[i][j] == t[j][i] for all elements in tables of t.

Other

  • remove (t, k)
Removes the element from t with k as its key; returns the corresponding value, or nil if no such element exists. Other table elements are unaffected. Provided as a table equivalent to table.remove.
  • sorted_pairs (t, g)
Returns a generator which iterates through the key-value pairs of t, sorted by g, which accepts the values of two pairs to compare followed by their corresponding keys. If g is not given, the pairs are sorted using Lua's less-than operator on the keys, as if by function (v1, v2, k1, k2) return k1 < k2 end.

See also

local pairs = pairs

local from_lists = function (ks, vs)
	local z = {}
	for i = 1, #ks do
		z[ks[i]] = vs[i]
	end
	return z
end

local from_ipairs = function (s, g)
	local z = {}
	for i = 1, #s do
		local k, v = g(s[i], i)
		if k ~= nil then
			z[k] = v
		end
	end
	return z
end

local from_pairs = function (t, g)
	local z = {}
	for k, v in pairs(t) do
		local k2, v2 = g(v, k)
		if k2 ~= nil then
			z[k2] = v2
		end
	end
	return z
end

local generate = function (s, g)
	local z = {}
	for i = 1, #s do
		z[s[i]] = g(s[i], i)
	end
	return z
end

local size = function (t)
	local z = 0
	for _ in pairs(t) do
		z = z + 1
	end
	return z
end

local any = function (t, pr)
	for k, v in pairs(t) do
		if pr(v, k) then
			return true
		end
	end
	return false
end

local all = function (t, pr)
	for k, v in pairs(t) do
		if not pr(v, k) then
			return false
		end
	end
	return true
end

local none = function (t, pr)
	for k, v in pairs(t) do
		if pr(v, k) then
			return false
		end
	end
	return true
end

local count = function (t, x)
	local z = 0
	for _, v in pairs(t) do
		if v == x then
			z = z + 1
		end
	end
	return z
end

local count_if = function (t, pr)
	local z = 0
	for k, v in pairs(t) do
		if pr(v, k) then
			z = z + 1
		end
	end
	return z
end

local find = function (t, x)
	for k, v in pairs(t) do
		if v == x then
			return k, v
		end
	end
end

local find_if = function (t, pr)
	for k, v in pairs(t) do
		if pr(v, k) then
			return k, v
		end
	end
end

local clone = function (t)
	local z = {}
	for k, v in pairs(t) do
		z[k] = v
	end
	return z
end

local map = function (t, f)
	local z = {}
	for k, v in pairs(t) do
		z[k] = f(v, k)
	end
	return z
end

local map_self = function (t, f)
	for k, v in pairs(t) do
		t[k] = f(v, k)
	end
	return t
end

local zip = function (t1, t2, op)
	local z = {}
	for k, v in pairs(t1) do
		z[k] = op(v, t2[k])
	end
	return z
end

local select = function (t, pr)
	local z = {}
	for k, v in pairs(t) do
		if pr(v, k) then
			z[k] = v
		end
	end
	return z
end

local merge_self = function (t1, t2, g)
	if g then
		for k, v in pairs(t2) do
			if t1[k] ~= nil then
				t1[k] = g(t1[k], v, k)
			else
				t1[k] = v
			end
		end
	else
		for k, v in pairs(t2) do
			t1[k] = v
		end
	end
	return t1
end

local merge = function (t1, t2, g)
	return merge_self(clone(t1), t2, g)
end

local invert = function (t)
	local z = {}
	for k, v in pairs(t) do
		z[v] = k
	end
	return z
end

local keys = function (t)
	local z = {}
	for k in pairs(t) do
		z[#z + 1] = k
	end
	return z
end

local values = function (t)
	local z = {}
	for _, v in pairs(t) do
		z[#z + 1] = v
	end
	return z
end

local to_lists = function (t)
	local ks = {}
	local vs = {}
	for k, v in pairs(t) do
		ks[#ks + 1] = k
		vs[#vs + 1] = v
	end
	return ks, vs
end

local transpose = function (t)
	local z = {}
	for k1, v1 in pairs(t) do
		for k2, v2 in pairs(v1) do
			z[k2] = z[k2] or {}
			z[k2][k1] = v2
		end
	end
	return z
end

local remove = function (t, k)
	local z = t[k]
	t[k] = nil
	return z
end

local sorted_pairs = function (t, f)
	local ks = keys(t)
	if f then
		table.sort(ks, function (k1, k2)
			return f(t[k1], t[k2], k1, k2)
		end)
	else
		table.sort(ks)
	end
	local i = 0
	return function ()
		i = i + 1
		if i <= #ks then
			return ks[i], t[ks[i]]
		end
	end
end

return {
	from_lists = from_lists,
	from_ipairs = from_ipairs,
	from_pairs = from_pairs,
	generate = generate,

	size = size,
	any = any,
	all = all,
	none = none,
	count = count,
	count_if = count_if,

	find = find,
	find_if = find_if,

	clone = clone,
	map = map,
	map_self = map_self,
	zip = zip,
	select = select,
	merge = merge,
	merge_self = merge_self,
	invert = invert,
	keys = keys,
	values = values,
	to_lists = to_lists,
	transpose = transpose,

	remove = remove,
	sorted_pairs = sorted_pairs,
}
Advertisement