Module:HashUtil/doc

Lua metamodule containing higher-order functions for non-sequence tables.


 * : Table which contains arbitrary key-value pairs.
 * : Function which accepts a value from a table as its first argument and the value's corresponding key as its second argument.
 * : 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.
 * : Function which accepts two table values.

All functions on tables use  to access their elements and therefore support the   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  on each value-key pair of the sequence  ; the first two value returned from   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  on each value-key pair of an arbitrary table  ; the first two value returned from   are then used to form key-value pairs in the result table. Nil keys are ignored.


 * generate (s, g)
 * Returns.

Counting

 * size (t)
 * Returns the number of key-value pairs in.


 * any (t, pr)
 * Returns true if  is true for any element of.


 * all (t, pr)
 * Returns true if  is true for all elements of.


 * none (t, pr)
 * Returns true if  is true for none of the elements of.


 * count (t, x)
 * Returns the number of values of  that compare equal to.


 * count_if (t, pr)
 * Returns the number of elements of  that satisfy.

Searching

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


 * find_if (t, pr)
 * Returns the key-value pair of any of the table's elements that satisfies, or   if none can be found.

Transformation

 * clone (t)
 * Returns a shallow copy of.


 * map (t, f)
 * Returns a new table where each value is replaced with the result of calling  on that value-key pair. Keys are unchanged.


 * map_self (t, f)
 * Replaces every value of  with the result of calling   on that value-key pair. Returns.


 * zip (t1, t2, op)
 * Returns a new table where the value for each key in  is the result of calling   on that value and the corresponding value in   with the same key (might be  ).


 * select (t, pr)
 * Returns a new table containing only the elements of  that satisfy.


 * merge (t1, t2, g)
 * Returns a new table containing key-value pairs from both tables.  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   is used to produce the value in the returned table. If not given, values in   take precedence, as if by.


 * merge_self (t1, t2, g)
 * Adds all key-value pairs from  to  . Returns  .   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 . The orders from the two sequences are guaranteed to be the same.


 * transpose (t)
 * Given a table-of-tables, returns a new table   with row indices and column indices swapped such that   for all elements in tables of.

Other

 * remove (t, k)
 * Removes the element from  with   as its key; returns the corresponding value, or   if no such element exists. Other table elements are unaffected. Provided as a table equivalent to.


 * sorted_pairs (t, g)
 * Returns a generator which iterates through the key-value pairs of, sorted by  , which accepts the values of two pairs to compare followed by their corresponding keys. If   is not given, the pairs are sorted using Lua's less-than operator on the keys, as if by.