TruWorlds

Table

Accessed via table

The table module provides functions for working with tables in TruScript.

Functions

table.count(table: table, function: function = nil) Returns number

Returns the number of entries in the table. When a function is given, it returns the number of entries for which function(value, key) is truthy.

table.has(table: table, key: any) Returns boolean

Returns true if the table contains the specified key, and false otherwise.

table.contains(table: table, value: any) Returns boolean

Returns true if any entry in the table has the specified value, and false otherwise.

table.keys(table: table) Returns array

Returns an array containing the keys of the table.

table.values(table: table) Returns array

Returns an array containing the values of the table.

table.remove(table: table, key: any)

Removes the entry with the specified key from the table, if present.

table.clear(table: table)

Removes all entries from the table.

table.map(table: table, function: function) Returns table

Returns a new table with the same keys, where each value is the result of calling function(value, key) on the original entry.

table.filter(table: table, function: function) Returns table

Returns a new table containing only the entries for which function(value, key) returns a truthy value.

table.reduce(table: table, function: function, seed: any) Returns any

Combines the table's entries into a single value. Starting from the seed, the accumulator is updated by calling function(accumulator, value, key) for each entry, and the final accumulator is returned.

table.find(table: table, function: function) Returns any

Returns the first value for which function(value, key) returns a truthy value, or nil if no entry matches.

table.for_each(table: table, function: function)

Calls function(value, key) for each entry in the table.

table.some(table: table, function: function) Returns boolean

Returns true if function(value, key) returns a truthy value for at least one entry, and false otherwise.

table.every(table: table, function: function) Returns boolean

Returns true if function(value, key) returns a truthy value for every entry, and false otherwise. Returns true for an empty table.