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
.
-
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, ', '))
-
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, ', '))
-
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))
-
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
-
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:
-
the maximum value in
t
, or nil
if t
is empty
-
number
the number of keys paired with the maximum value
-
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) print(tiedCount) print(table.concat(TableUtils:sortedKeys(tiedCount), ', '))
-
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
-
TableUtils:sample (l)
-
Choose a random value from a list.
Parameters:
Returns:
-
some
l[i]
such that i
is in [1, #l]
, or nil
if #l < 1
-
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:
-
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:
-
function
iterator
-
table
list
-
number
index
Usage:
local TableUtils = require "util.table"
local l = {'a', 'b', 'c'}
for i, v in TableUtils:ipairsReversed(l) do
print(i, v) end
-
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:
-
boolean
whether
ta
and tb
are equal
-
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