Module wonderful.util.table
Various table utilities.
Functions
shallowcopy(orig) | Create a shallow copy of a table. |
shalloweq(lhs, rhs) | Test two values for equality. |
isin(value, tbl) | Checks if a table contains a value. |
autoimport(root, pkg) | Create a table that tries to require a submodule when indexed. |
swapPairs(tbl) | Swap the keys and values of a table. |
tableLen(tbl) | Calculate the number of entries in a table. |
getKeys(tbl) | Get keys of a table. |
nextEntry(tbl[, key]) | Get an element of a table that follows a previous one. |
prevEntry(tbl[, key]) | Get an element of a table that precedes a following one. |
removeFirst(tbl, value) | Find the first sequence entry whose value equals to the given one, and remove it. |
first(tbl, predicate[, start=1[, stop=#tbl]]) | Find the first sequence entry that satisfies a predicate. |
last(tbl, predicate[, start=1[, stop=#tbl]]) | Find the last sequence entry that satisfies a predicate. |
Functions
- shallowcopy(orig)
-
Create a shallow copy of a table.
Parameters:
- orig table a value
Returns:
-
table
the copy
- shalloweq(lhs, rhs)
-
Test two values for equality.
If both values are tables, checks whether the left value is a subset of
the right value, using
==
to test for equality of table values.Parameters:
- lhs the left value
- rhs the right value
Returns:
-
boolean
- isin(value, tbl)
-
Checks if a table contains a value.
Parameters:
- value a value
- tbl table a table
Returns:
-
true
if the table contains the given value - the first key which maps to the given value
Or
false
if the table doesn't contain the given value - autoimport(root, pkg)
-
Create a table that tries to require a submodule when indexed.
E. g.,
autoimport({}, "test").submodule
tries torequire("test.submodule")
and returns the value if it succeeds.If the required value is a table, applies autoimport to it.
Parameters:
Returns:
- swapPairs(tbl)
-
Swap the keys and values of a table.
Parameters:
- tbl table a table
Returns:
-
table
a swapped table
Raises:
the table has two keys with the same valueUsage:
local tbl = {13, 19, 20, 45} local swapped = swapPairs(tbl) print(swapped[13]) --> 1 print(swapped[1]) --> nil
- tableLen(tbl)
-
Calculate the number of entries in a table.
Parameters:
- tbl table the table
Returns:
-
int
the number of entries
- getKeys(tbl)
-
Get keys of a table.
Parameters:
- tbl table the table
Returns:
-
table
a table of the keys
- nextEntry(tbl[, key])
-
Get an element of a table that follows a previous one.
If the given key is
nil
, the first element is returned.Parameters:
- tbl table the table
- key the key of the previous element (optional)
Returns:
- the next key
- the next element
- prevEntry(tbl[, key])
-
Get an element of a table that precedes a following one.
If the given key is
nil
, the last element is returned.Parameters:
- tbl table the table
- key the key of the following element (optional)
Returns:
- the preceding key
- the preceding element
- removeFirst(tbl, value)
-
Find the first sequence entry whose value equals to the given one, and
remove it.
Parameters:
- tbl table the sequence
- value the value
Returns:
- the key of the removed entry
- the value of the removed entry
Or
-
nil
no entry was found
- first(tbl, predicate[, start=1[, stop=#tbl]])
-
Find the first sequence entry that satisfies a predicate.
The predicate should return any value other than
nil
andfalse
to stop the iteration and return the current value.The range of search is
[start; stop]
.Parameters:
- tbl table the sequence
- predicate function(element,key,tbl) the predicate
- start int the index to start searching from (default 1)
- stop int the index to stop searching at (default #tbl)
Returns:
- the entry key
- the entry value
Or
-
nil
no entries satisfy the predicate
- last(tbl, predicate[, start=1[, stop=#tbl]])
-
Find the last sequence entry that satisfies a predicate.
The predicate should return any value other than
nil
andfalse
to stop the iteration and return the current value.The range of search is
[start; stop]
.Parameters:
- tbl table the sequence
- predicate function(element,key,tbl) the predicate
- start int the start index (default 1)
- stop int the end index (default #tbl)
Returns:
- the entry key
- the entry value
Or
-
nil
no entries satisfy the predicate