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:

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:

  1. true if the table contains the given value
  2. 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 to require("test.submodule") and returns the value if it succeeds.

If the required value is a table, applies autoimport to it.

Parameters:

  • root table an root table
  • pkg string a root module name

Returns:

    table
swapPairs(tbl)
Swap the keys and values of a table.

Parameters:

Returns:

    table a swapped table

Raises:

the table has two keys with the same value

Usage:

    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:

Returns:

    int the number of entries
getKeys(tbl)
Get keys of a table.

Parameters:

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:

  1. the next key
  2. 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:

  1. the preceding key
  2. 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:

  1. the key of the removed entry
  2. 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 and false 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:

  1. the entry key
  2. 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 and false 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:

  1. the entry key
  2. the entry value

Or

    nil no entries satisfy the predicate
generated by LDoc 1.4.6 Last updated 2019-07-27 21:32:05