Module utils.table
Table utilities.
There are two ways to call these functions. Either as function, or as chainable methods. The latter can make your code shorter and easier to read.
E.g., instead of:
table.concat( table.sort( utils.table.map( utils.table.keys(words), string.lower ) ), " " )
you can do:
local List = utils.table.List List(words) :keys() :map(string.lower) :sort() :concat(" ")
Functions
List([t]) | “Spices us” a table to support chainable methods. |
count(t) | Count the number of elements. |
filter(t, predicate) | Filters values. |
find(t, predicate) | Finds a value. |
imap(t, fn) | Maps over a sequence. |
iterate(t) | Iterates over a sequence values. |
keys(t) | Returns the keys of a table. |
makeset(t) | Converts a table to a set. |
map(t, fn) | Maps over a table. |
sub(t, first[, last]) | Extracts a part of a table. |
Functions
- List([t])
-
“Spices us” a table to support chainable methods.
The methods that will be available are the functions mentioned on this page plus the following from standard Lua:
insert
,remove
,sort
,concat
.Argument t may be a table, an iterator function, or nothing (which is the same as an empty table).
- count(t)
-
Count the number of elements.
Useful for non-sequences only (use
#t
otherwise!). - filter(t, predicate)
-
Filters values.
Returns a new table with only the values that satisfied function predicate. The table t is assumed to be a sequence.
- find(t, predicate)
-
Finds a value.
Returns the first element that satisfy function predicate. The table t is assumed to be a sequence.
- imap(t, fn)
-
Maps over a sequence.
Returns a new table with the results of applying fn to the elements
t[1], …, t[#t]
. - iterate(t)
- Iterates over a sequence values.
- keys(t)
- Returns the keys of a table.
- makeset(t)
-
Converts a table to a set.
Returns a new table whose keys are the values of the original table. The new values are all
true
. - map(t, fn)
-
Maps over a table.
Returns a new table with the keys preserved and the values the result of applying fn to the original values.
- sub(t, first[, last])
-
Extracts a part of a table.
Behaves just like Lua’s string.sub except that it operates on a sequence. Negative indexes count from the end of the sequence.