Module util.table

Useful operations on tables.

For arbitrary tables, we'll use the letter t.

For list-like tables, however, we'll use the letter l.

Lists

TableUtils:filter (l, f) Filter values of a list.
TableUtils:map (l, m) Map values of a list.
TableUtils:foldl (f, z, l) Perform a left fold of a list with a combining function and an initial value.
TableUtils:reverse (l) Reverse a list.
TableUtils:histogram (l) Create histogram of values of a list.

Arithmetic

TableUtils:sum (t) Sum all values of a table.
TableUtils:numOfPairs (t) Get the number of key-value pairs in a table.
TableUtils:podium (t) Create a podium for a table of comparable values.

Keys

TableUtils:sortedKeys (t) Create an ordered list with all the keys of a table.

Random

TableUtils:sample (l) Choose a random value from a list.
TableUtils:uniqueSamples (l, m) Sample m values from a list (without replacement).
TableUtils:shuffleInPlace (l) Shuffle the values of a list in-place.

Iteration

TableUtils:sortedIter (t, f) Iterate through the pairs of a table, ordered by the keys.
TableUtils:ipairsReversed (l) Iterate through all index-value pairs of a list, in reverse order.

Recursion

TableUtils:deepEqual (ta, tb[, checkmetatable=false]) Recursively check if two tables are equal.
TableUtils:deepClone (t) Recursively clone a table.


Lists

TableUtils:filter (l, f)
Filter values of a list.

Parameters:

  • l table
  • f function filter

Returns:

    table list containing only pairs (i, v) for which f(v) is true.

Usage:

    local TableUtils = require "util.table"
    local l = {1, 2, 3, 4, 5, 6}
    local function f (v) return v % 2 == 0 end
    local fl = TableUtils:filter(l, f)
    print(table.concat(fl, ', ')) -- 2, 4, 6
TableUtils:map (l, m)
Map values of a list.

Parameters:

  • l table
  • m function mapping

Returns:

    table list containing only pairs (i, m(v)) for all (i, v) in l.

Usage:

    local TableUtils = require "util.table"
    local l = {1, 2, 3, 4, 5, 6}
    local function m (v) return v * 2 end
    local ml = TableUtils:map(l, m)
    print(table.concat(ml, ', ')) -- 2, 4, 6, 8, 10, 12
TableUtils:foldl (f, z, l)
Perform a left fold of a list with a combining function and an initial value.

Parameters:

  • f function combining function
  • z initial value
  • l table list

Returns:

    final state of the accumulator

Usage:

    local TableUtils = require "util.table"
    local l = {'a', 'b', 'c'}
    print(TableUtils:foldl(string.concat, '', l)) -- abc
TableUtils:reverse (l)
Reverse a list.

Parameters:

Returns:

    table a list with all values of l in reverse order
TableUtils:histogram (l)
Create histogram of values of a list.

Parameters:

Returns:

    table histogram of values of l

Arithmetic

TableUtils:sum (t)
Sum all values of a table.

Parameters:

Returns:

    number sum of all values of t
TableUtils:numOfPairs (t)
Get the number of key-value pairs in a table.

Parameters:

Returns:

    number number of key-value pairs in t
TableUtils:podium (t)
Create a podium for a table of comparable values.

Parameters:

Returns:

  1. the maximum value in t, or nil if t is empty
  2. number the number of keys paired with the maximum value
  3. table the set of keys paired with the maximum value

Usage:

    local TableUtils = require "util.table"
    local t = {a=5, b=7, c=3, d=7, e=1}
    local maxValue, tiedCount, tiedKeys = TableUtils:podium(t)
    print(maxValue) -- 7
    print(tiedCount) -- 2
    print(table.concat(TableUtils:sortedKeys(tiedCount), ', ')) -- b, d

Keys

TableUtils:sortedKeys (t)
Create an ordered list with all the keys of a table. Orders keys by < on the key types, and then by < on the keys.

Parameters:

Returns:

    table an ordered list of all the keys of t

Random

TableUtils:sample (l)
Choose a random value from a list.

Parameters:

Returns:

  1. some l[i] such that i is in [1, #l], or nil if #l < 1
  2. optional number i, iff #l >= 1

See also:

TableUtils:uniqueSamples (l, m)
Sample m values from a list (without replacement). Asserts that m is less than or equal to the length of the list.

Parameters:

Returns:

    table a list containing m unique samples from l

See also:

TableUtils:shuffleInPlace (l)
Shuffle the values of a list in-place.

Parameters:

See also:

Iteration

TableUtils:sortedIter (t, f)
Iterate through the pairs of a table, ordered by the keys. Calls f for all key-value pairs and checks if the return value is true. If f(k, v) ever returns some ret different from nil and false, then the iteration stops and ret is returned.

Parameters:

Returns:

    the first true return value of f(k, v) or nil
TableUtils:ipairsReversed (l)
Iterate through all index-value pairs of a list, in reverse order.

Parameters:

Returns:

  1. function iterator
  2. table list
  3. number index

Usage:

    local TableUtils = require "util.table"
    local l = {'a', 'b', 'c'}
    for i, v in TableUtils:ipairsReversed(l) do
      print(i, v) -- 3 c; 2 b; 1 a
    end

Recursion

TableUtils:deepEqual (ta, tb[, checkmetatable=false])
Recursively check if two tables are equal. You can also check for metatable equality with checkmetatable.

Parameters:

  • ta table
  • tb table
  • checkmetatable boolean (default false)

Returns:

  1. boolean whether ta and tb are equal
  2. optional string an error message (if ta and tb are not equal)
TableUtils:deepClone (t)
Recursively clone a table. Also sets metatables.

Parameters:

Returns:

    table a clone of t
generated by LDoc 1.5.0 Last updated 2023-07-02 05:10:58