File lua/proputil.lua
Miscellaneous utilities for PropXX
Functions
assertBoolean (x) | Assert that the given argument is a boolean value. |
assertFunction (x) | Assert that the given argument is a function. |
assertNumber (x) | Assert that the given argument is a number. |
assertString (x) | Assert that the given argument is a string. |
assertTable (x) | Assert that the given argument is a table. |
checkFileName (file, mode) | Test whether a given file name is valid, i.e., the corresponding file can be opened. |
filteredPairs (filter, gen) | Create an iterator which returns only those elements of the iteration for which a given filter function returns false. |
genPairs (map) | Generator function which returns all (key, value) pairs in a given map. |
iprint (indent, ...) | Print with indentation (using io.write) |
iprintln (indent, ...) | Print with indentation and newline at end of output (using io.write) |
leafPairs (sep, gen) | Recursively iterate through (tree- or DAG-like) nested tables and returns only elementary values (leafs). |
mapPairs (map, gen) | Create an iterator which applies a function to the elements of a given iteration before returning them. |
serialize (object, name, gen, depth) | Serialize a given object to valid Lua code |
sortedPairs (gen, comp) | Create an iterator which returns the elements of a given iteration in sorted order |
table.append (self, other) | Concatenate two lists. |
table.assign (target, src) | Assign all elements in one table to another table. |
table.deepCopy (object) | Recursive copy of a table. |
table.merge (first, second) | Merge the elements of two tables into a new single table Elements with the same key in the first table are overwritten by elements of the second table . |
Functions
- assertBoolean (x)
-
Assert that the given argument is a boolean value. Throws an error if this is not true.
Parameters
- x: the object to be checked
- assertFunction (x)
-
Assert that the given argument is a function. Throws an error if this is not true.
Parameters
- x: the object to be checked
- assertNumber (x)
-
Assert that the given argument is a number. Throws an error if this is not true.
Parameters
- x: the object to be checked
- assertString (x)
-
Assert that the given argument is a string. Throws an error if this is not true.
Parameters
- x: the object to be checked
- assertTable (x)
-
Assert that the given argument is a table. Throws an error if this is not true.
Parameters
- x: the object to be checked
- checkFileName (file, mode)
-
Test whether a given file name is valid, i.e., the corresponding file can be opened.
Parameters
- file: the file name to be checked
- mode: the mode in which the file shall be opened (default: "r")
Return value:
true if the given file can be opened in the given mode - filteredPairs (filter, gen)
-
Create an iterator which returns only those elements of the iteration for which a given filter function returns false.
Parameters
- filter: a function that is applied to (key, value) pairs and which shall return true if the given pair shall be included in the filtered iteration
- gen: the iterator/generator for the original iteration (which generates (key, value) pairs on subsequent calls)
Return value:
an iterator/generator which returns the filtered (key, value) pairs - genPairs (map)
-
Generator function which returns all (key, value) pairs in a given map.
Parameters
- map: the map for which the pairs shall be returned
Return value:
a coroutine that returns all (key, value) pairs - iprint (indent, ...)
-
Print with indentation (using io.write)
Parameters
- indent: number of spaces to use for indentation
- ...:
- iprintln (indent, ...)
-
Print with indentation and newline at end of output (using io.write)
Parameters
- indent: number of spaces to use for indentation
- ...:
- leafPairs (sep, gen)
-
Recursively iterate through (tree- or DAG-like) nested tables and returns only elementary values (leafs). The nested table structure has to be acyclic.
Parameters
- sep: string which is used as separator for generating the full keys of the leaf values.
- gen: the iterator/generator for the original iteration (which generates (key, value) pairs on subsequent calls)
Return value:
an iterator/generator which returns (key, value) pairs where value is set to the elementary values in the given nested table and key is the key path to the corresponding value (Example: the table { foo = { bar = 1 } } contains the value 1 with key foo.bar. - mapPairs (map, gen)
-
Create an iterator which applies a function to the elements of a given iteration before returning them.
Parameters
- map: function which maps key/value pairs to mapped key/value pairs
- gen: the iterator/generator for the original iteration (which generates (key, value) pairs on subsequent calls)
Return value:
an iterator/generator which returns (key, value) pairs that are mapped using the given function - serialize (object, name, gen, depth)
-
Serialize a given object to valid Lua code
Parameters
- object: the objecto be serialized
-
name: the name of the object (optional). if non-empty, the output is written as
= - gen: the generator function for traversing the elements of the given object if this object is a table (optional, default: traverse in standard sorted order of the keys)
- depth: auxiliary parameter for the indentation level. used to pretty print the output. default is zero.
Return value:
the given object as string containing valid Lua-code. - sortedPairs (gen, comp)
-
Create an iterator which returns the elements of a given iteration in sorted order
Parameters
- gen: the iterator/generator for the original iteration (which generates (key, value) pairs on subsequent calls)
-
comp: comparator function which compairs key/value pairs stored in a map { k=
, v= } (optional with default comparator function that compares the keys using '<')
Return value:
an iterator/generator which returns (key, value) pairs in sorted order - table.append (self, other)
-
Concatenate two lists. The ipairs in other are added after the ipairs of self.
Parameters
- self: the table to which content shall be appended
- other: the table which contains the content to be appended
Return value:
the modified table self - table.assign (target, src)
-
Assign all elements in one table to another table. Elements with the same key in the target table are overwritten. Elements with keys that do not exist in the source table remain in the target table.
Parameters
- target: the target table to which the values of the source table shall be assigned.
- src: the source table which contains the values that shall be assigned to the target table.
- table.deepCopy (object)
-
Recursive copy of a table. Copies tables by copying their elements resp. recursively copying sub-tables. References to copied sub-tables are stored and reused if the copied structure contains multiple references to the same sub-table. Meta-tables are not copied.
Parameters
- object: the object to be copied
Return value:
the copy of the object - table.merge (first, second)
-
Merge the elements of two tables into a new single table Elements with the same key in the first table are overwritten by elements of the second table . Elements with keys that do not exist in the second table remain in the first table.
Parameters
- first: the first table to be merged
- second: the second table to be merged