Module util
Useful language constructs and helper functions.
Contains an OO class system implementation, enums, various iterators for range-based for loops, Sets, and other tidbits.
OO Support
class () | Defines a new base class. |
subclass (base) | Defines a subclass of an existing (sub)class. |
Enumerations
enum (names) | Creates an enumeration from an array of strings. |
Iterators
iter (t[, order[, extr]]) | Generic table iterator. |
keys (t) | Iterate over table keys, unsorted. |
skeysk (t[, order]) | Iterate over keys, sorted by keys (default ascending order, can be overridden) |
skeysv (t[, order]) | Iterate over keys, sorted by values (default ascending order, can be overridden) |
values (t) | Iterate over table values, unsorted. |
svaluesk (t[, order]) | Iterate over values, sorted by keys (default ascending order, can be overridden) |
svaluesv (t[, order]) | Iterate over values, sorted by values (default ascending order, can be overridden) |
spairsk (t[, order]) | Iterate over key/value pairs, sorted by keys (default ascending order, can be overridden) |
spairsv (t[, order]) | Iterate over key/value pairs, sorted by values (default ascending order, can be overridden) |
Sets
Set.add (s, o) | Add an element to set. |
Set.del (s, o) | Remove an element from set. |
Set.clear (s) | Empties the set. |
Set.make (list) | Make a set from an array. |
Set.values (s) | Iterates over values of the set. |
Set.mkvalues (list) | Iterates over the given items. |
Functions
is_member (x, y) | Tests membership in array. |
copy_fields (src, dest) | Copies contents of one table into another. |
print (t[, depth=1000[, indent=""]]) | Recursively print table contents. |
empty (t) | Returns if table is empty. |
count (t) | Returns size of table. |
unique_key (t, base) | Generates a unique table key from a prefix. |
con_cat (...) | Concatenate arguments with underscores. |
dot_to_uscore (str) | Replace all dots in a string with underscores. |
OO Support
A simple class system. See Builder for an example of its use.
- class ()
-
Defines a new base class.
The returned table, representing the class, will have an empty constructor, which is a method
named
__ctor
. It can be overridden with your own constructor. To actually instantiate the class, one must call the.new
static method.Returns:
Usage:
foo = class() function foo:__ctor(x,y,z) ... end function foo:mymethod() ... end foo_inst = foo.new(xparam,yparam,zparam) foo_inst:mymethod()
- subclass (base)
-
Defines a subclass of an existing (sub)class.
Parameters:
- base table base class
Returns:
Usage:
bar = subclass(foo) bar:mymethod()
Enumerations
- enum (names)
-
Creates an enumeration from an array of strings.
This is a bidirectional mapping between the strings and their
associated unique integers. The strings are pre-processed and made lowercase.
Parameters:
- names array an array of unique strings
Returns:
-
table
a bidirectional mapping of strings to integers
Iterators
Used for range-based
for
loops.
Usage:
t = { ['a']=1, [3.1415]='hello', ['beef']='jerky' } for v in values(t) do ... end
- iter (t[, order[, extr]])
-
Generic table iterator.
Used by specialized iterators.
Parameters:
- t table
- order
function(table,a,b)
returns true if
a
should precedeb
(optional) - extr func (k,v) extractor function (optional)
Returns:
-
a for-loop iterator that iterates over
t
using an optional ordering, returning akey,val
pair on each iteration, or returningextr(key,val)
instead ifextr
was specified - keys (t)
-
Iterate over table keys, unsorted.
Parameters:
- t table
Returns:
-
iterator
- skeysk (t[, order])
-
Iterate over keys, sorted by keys (default ascending order, can be overridden)
Parameters:
- t table
- order
function(a,b)
returns true if
a
should precedeb
(optional)
Returns:
-
iterator
- skeysv (t[, order])
-
Iterate over keys, sorted by values (default ascending order, can be overridden)
Parameters:
- t table
- order
function(a,b)
returns true if
a
should precedeb
(optional)
Returns:
-
iterator
- values (t)
-
Iterate over table values, unsorted.
Parameters:
- t table
Returns:
-
iterator
- svaluesk (t[, order])
-
Iterate over values, sorted by keys (default ascending order, can be overridden)
Parameters:
- t table
- order
function(a,b)
returns true if
a
should precedeb
(optional)
Returns:
-
iterator
- svaluesv (t[, order])
-
Iterate over values, sorted by values (default ascending order, can be overridden)
Parameters:
- t table
- order
function(a,b)
returns true if
a
should precedeb
(optional)
Returns:
-
iterator
- spairsk (t[, order])
-
Iterate over key/value pairs, sorted by keys (default ascending order, can be overridden)
Parameters:
- t table
- order
function(a,b)
returns true if
a
should precedeb
(optional)
Returns:
-
iterator
- spairsv (t[, order])
-
Iterate over key/value pairs, sorted by values (default ascending order, can be overridden)
Parameters:
- t table
- order
function(a,b)
returns true if
a
should precedeb
(optional)
Returns:
-
iterator
Sets
Set-like tables.
Stored elements comprise the keys, and the values are just dummy
true
s.
Faciliates easy addition, removal, membership testing.
Functions are stored in a table called Set. This is not a class.
- Set.add (s, o)
-
Add an element to set.
Parameters:
- s table set
- o object
- Set.del (s, o)
-
Remove an element from set.
Parameters:
- s table set
- o object
- Set.clear (s)
-
Empties the set.
Parameters:
- s table set
- Set.make (list)
-
Make a set from an array.
Calls Set.add on each element.
Parameters:
- list array
Returns:
-
set
- Set.values (s)
-
Iterates over values of the set.
Parameters:
- s table set
Returns:
-
iterator
- Set.mkvalues (list)
-
Iterates over the given items.
Probably equivalent to calling values on an array directly.
Parameters:
- list array
Returns:
-
iterator
Functions
The rest.
- is_member (x, y)
-
Tests membership in array.
Parameters:
- x array an array (NOT a set)
- y value (not key!) to search for
Returns:
-
bool
- copy_fields (src, dest)
-
Copies contents of one table into another.
Parameters:
- print (t[, depth=1000[, indent=""]])
-
Recursively print table contents.
It's smart! Avoids following circular references.
Also prints non-table objects, but nothing special is done for them.
Parameters:
- empty (t)
-
Returns if table is empty.
Parameters:
- t table
Returns:
-
boolean
- count (t)
-
Returns size of table.
Table doesn't have to be an array.
Parameters:
- t table
Returns:
-
number
- unique_key (t, base)
-
Generates a unique table key from a prefix.
If
base
is "foo", then this will return the first "fooN" that does not yet exist as a key in the table, where N is an integer.Parameters:
Returns:
- con_cat (...)
-
Concatenate arguments with underscores.
Parameters:
- ... a bunch of strings
Returns:
- dot_to_uscore (str)
-
Replace all dots in a string with underscores.
This is useful for operating on hierarchical names.
Parameters:
- str input string
Returns: