4,466 bytes added
, 05:56, 16 July 2021
{{module rating|alpha}}
This module includes a number of set operations for Lua tables. It currently has [[Union (set theory)|union]], [[Intersection (set theory)|intersection]] and [[Complement (set theory)|complement]] functions for both key/value pairs and for values only. It is a meta-module, meant to be called from other Lua modules, and should not be called directly from #invoke.
== Loading the module ==
To use any of the functions, first you must load the module.
<syntaxhighlight lang="lua">
local set = require('Module:Set')
</syntaxhighlight>
== union ==
<syntaxhighlight lang="lua">
set.union(t1, t2, ...)
</syntaxhighlight>
Returns the union of the key/value pairs of n tables. If any of the tables contain different values for the same table key, the table value is converted to an array holding all of the different values. For example, for the tables <code style="white-space: nowrap;">{foo = "foo", bar = "bar"}</code> and <code style="white-space: nowrap;">{foo = "foo", bar = "baz", qux = "qux"}</code>, union will return <code style="white-space: nowrap;">{foo = "foo", bar = {"bar", "baz"}, qux = "qux"}</code>. An error is raised if the function receives less than two tables as arguments.
== valueUnion ==
<syntaxhighlight lang="lua">
set.valueUnion(t1, t2, ...)
</syntaxhighlight>
Returns the union of the values of n tables, as an array. For example, for the tables <code style="white-space: nowrap;">{1, 3, 4, 5, foo = 7}</code> and <code style="white-space: nowrap;">{2, bar = 3, 5, 6}</code>, valueUnion will return <code style="white-space: nowrap;">{1, 2, 3, 4, 5, 6, 7}</code>. An error is raised if the function receives less than two tables as arguments.
== intersection ==
<syntaxhighlight lang="lua">
set.intersection(t1, t2, ...)
</syntaxhighlight>
Returns the intersection of the key/value pairs of n tables. Both the key and the value must match to be included in the resulting table. For example, for the tables <code style="white-space: nowrap;">{foo = "foo", bar = "bar"}</code> and <code style="white-space: nowrap;">{foo = "foo", bar = "baz", qux = "qux"}</code>, intersection will return <code style="white-space: nowrap;">{foo = "foo"}</code>. An error is raised if the function receives less than two tables as arguments.
== valueIntersection ==
<syntaxhighlight lang="lua">
set.valueIntersection(t1, t2, ...)
</syntaxhighlight>
Returns the intersection of the values of n tables, as an array. For example, for the tables <code style="white-space: nowrap;">{1, 3, 4, 5, foo = 7}</code> and <code style="white-space: nowrap;">{2, bar = 3, 5, 6}</code>, valueIntersection will return <code style="white-space: nowrap;">{3, 5}</code>. An error is raised if the function receives less than two tables as arguments.
== complement ==
<syntaxhighlight lang="lua">
set.complement(t1, t2, ..., tn)
</syntaxhighlight>
Returns the [[relative complement]] of <code>''t1''</code>, <code>''t2''</code>, ..., in <code>''tn''</code>. The complement is of key/value pairs. This is equivalent to all the key/value pairs that are in <code>''tn''</code> but are not in any of <code>''t1''</code>, <code>''t2''</code>, ... <code>''tn-1''</code>. For example, for the tables <code style="white-space: nowrap;">{foo = "foo", bar = "bar", baz = "baz"}</code> and <code style="white-space: nowrap;">{foo = "foo", bar = "baz", qux = "qux"}</code>, complement would return <code style="white-space: nowrap;">{bar = "baz", qux = "qux"}</code>. An error is raised if the function receives less than two tables as arguments.
== valueComplement ==
<syntaxhighlight lang="lua">
set.valueComplement(t1, t2, ...)
</syntaxhighlight>
This returns an array containing the [[relative complement]] of <code>''t1''</code>, <code>''t2''</code>, ..., in <code>''tn''</code>. The complement is of values only. This is equivalent to all the values that are in tn but are not in t1, t2, ... tn-1. For example, for the tables <code style="white-space: nowrap;">{1, 2}</code>, <code style="white-space: nowrap;">{1, 2, 3}</code> and <code style="white-space: nowrap;">{1, 2, 3, 4, 5}</code>, valueComplement would return <code style="white-space: nowrap;">{4, 5}</code>. An error is raised if the function receives less than two tables as arguments.
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox||{{#ifeq:{{SUBPAGENAME}}|testcases||
<!--Categories below here, please; interwikis to Wikidata.-->
[[Category:Lua metamodules]]
}}}}</includeonly>