Module:HashUtil/doc

From Langrisser Wiki
Jump to: navigation, search

This is the documentation page for Module:HashUtil

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 elements.

Generation[edit source]

  • from_lists (ks, vs)
Creates a table from a list of keys and a list of values. Later duplicate keys take precedence.
  • from_pairs (s, g)
Creates a table by calling g on each element of the sequence s and using its first two return values as a key-value pair.
  • generate (s, g)
Returns {[s[1]] = g(s[1]), [s[2]] = g(s[2]), ..., [s[#s]] = g(s[#s])}.

Counting[edit source]

  • 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 elements of t that compare equal to x.
  • count_if (t, pr)
Returns the number of elements of t that satisfy pr.

Searching[edit source]

  • find (t, x)
Returns the key-value pair of any of the table's elements that 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[edit source]

  • map (t, f)
Returns a new table with the results of calling f on each element of t.
  • map_self (t, f)
Replaces every element of t with the result of calling f on it. Returns t.
  • zip (t1, t2, op)
Returns a new table with the results of calling op on each element of t1 and the corresponding element of t2 with the same key.
  • select (t, pr)
Returns a new table containing only the elements of t that satisfy pr.
  • merge (t1, t2)
Returns a new table containing key-value pairs from both tables. t2 takes precedence.
  • merge_self (t1, t2)
Adds all key-value pairs from t2 to t1. Returns t1.
  • 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.

Other[edit source]

  • sorted_pairs (t)
Returns a generator which iterates through the key-value pairs of t, sorted by keys with <.
Equivalent to sorted_pairs(t, function (v1, v2, k1, k2) return k1 < k2 end).
  • 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.