Module util

Utilities for lua programming.

Functions

append (first, ...) Appends the elements in any number of additional tables to the first table.
callable (x) Is x a callable type?
cathash (...) Creates a single table from any number of input tables.
catlist (...) Creates a single list from any number of lists.
contains (haystack, needle) Check if a table contains an item
copy_table (object) Copy a table.
curry (fn, ...) Curry a function
defclass (name) Define a new class
exists (list, pred) Test list for at least one value matching pred
filter (fn, list) Filter a list based on fn
forall (list, pred) Test every list element against pred
foreach (list, fn) Apply a function to each value in a table.
identity (x) Identity function.
indexof (list, item) Find the key of item
join (sep, list) Creates a string of the elements in list joined by separator.
keys (map) Returns a list of the keys in the given map.
map (fn, ...) Classic map, but discards nil values (table.insert doesn't like nil).
namespace (table_name) Initialises a namespace that has functions spread across multiple files.
newinstance (class) Instatiate a new object
pairs (map) Make a list of pairs from a map.
partition (list, slice[, increment=slice]) Partition a list.
random_branch_weighted (list)

Select randomly from a list based on branch and weight The list should be a list of tables with any of:

  • Keys for each branch name specifying the weight in that branch.
random_choose_weighted (list) Given a list of pairs, where the second element of each pair is an integer weight, randomly choose from the list.
random_from (list) Pick a random element from a list (unweighted)
random_range_real (lower, upper) Random real number in the range [lower,upper)
random_subset (set, n) Given a table of elements, choose a subset of size n without replacement.
random_weighted_from (weightfn, list) Pick a random element from a list based on a weight function
random_weighted_keys (weightfn, list[, order]) Pick a random key from a table using weightfn weightfn is used as in util.random_weighted_from
remove (list, item) Remove an item
set (list) Creates a set (a map of keys to true) from the list supplied.
slice (list, istart, iend) Slice a list.
sort (t, f) A wrapper on table.sort that uses locale-insensitive comparison for strings by default.
stable_lessthan (x1, x2) a locale-insensitive less-than operation.
subclass (parent, subclassname) Define a subclass
trim (s) Remove leading and trailing whitespace From http://lua-users.org/wiki/CommonFunctions
values (map) Returns a list of the values in the given map.


Functions

append (first, ...)
Appends the elements in any number of additional tables to the first table.

Parameters:

  • first
  • ...
callable (x)
Is x a callable type?

Parameters:

  • x
cathash (...)
Creates a single table from any number of input tables. Keys get overwritten in the merge.

Parameters:

  • ...
catlist (...)
Creates a single list from any number of lists.

Parameters:

  • ...
contains (haystack, needle)
Check if a table contains an item

Parameters:

  • haystack
  • needle
copy_table (object)
Copy a table. Copied from http://lua-users.org/wiki/CopyTable

Parameters:

  • object
curry (fn, ...)
Curry a function

Parameters:

  • fn
  • ...
defclass (name)
Define a new class

Parameters:

  • name
exists (list, pred)
Test list for at least one value matching pred

Parameters:

  • list
  • pred
filter (fn, list)
Filter a list based on fn

Parameters:

  • fn
  • list
forall (list, pred)
Test every list element against pred

Parameters:

  • list
  • pred
foreach (list, fn)
Apply a function to each value in a table.

Parameters:

  • list
  • fn
identity (x)
Identity function.

Parameters:

  • x
indexof (list, item)
Find the key of item

Parameters:

  • list
  • item
join (sep, list)
Creates a string of the elements in list joined by separator.

Parameters:

  • sep
  • list
keys (map)
Returns a list of the keys in the given map.

Parameters:

  • map
map (fn, ...)
Classic map, but discards nil values (table.insert doesn't like nil).

Parameters:

  • fn
  • ...
namespace (table_name)
Initialises a namespace that has functions spread across multiple files. If the namespace table does not exist, it is created. If it already exists, it is not modified.

Parameters:

  • table_name
newinstance (class)
Instatiate a new object

Parameters:

  • class
pairs (map)
Make a list of pairs from a map.

Parameters:

  • map

Returns:

    a list of lists built from the given map, each sublist being in the form { key, value } for each key-value pair in the map.
partition (list, slice[, increment=slice])
Partition a list.

Parameters:

  • list the list
  • slice the slice size
  • increment the increment for slice start (default slice)

Returns:

    a list of pieces
random_branch_weighted (list)
Select randomly from a list based on branch and weight The list should be a list of tables with any of:

  • Keys for each branch name specifying the weight in that branch.
  • A single key branch containing a string, restricting the item to appear only in that branch
  • A single key weight giving the default weight.

An item with no keys gets a default weight of 10.

Parameters:

  • list A list of tables with weighting keys.
random_choose_weighted (list)
Given a list of pairs, where the second element of each pair is an integer weight, randomly choose from the list.

Parameters:

  • list the list of pairs to choose from
random_from (list)
Pick a random element from a list (unweighted)

Parameters:

  • list

See also:

random_range_real (lower, upper)
Random real number in the range [lower,upper)

Parameters:

  • lower
  • upper
random_subset (set, n)
Given a table of elements, choose a subset of size n without replacement.

Parameters:

  • set
  • n
random_weighted_from (weightfn, list)
Pick a random element from a list based on a weight function

Parameters:

  • weightfn function or key to choose weights
  • list if weightfn is a key then elements of list have weights chosen according to the number in key (default weight is 10).
random_weighted_keys (weightfn, list[, order])
Pick a random key from a table using weightfn weightfn is used as in util.random_weighted_from

Parameters:

  • weightfn function or key to choose weights
  • list the list
  • order order function to use on keys (optional)
remove (list, item)
Remove an item

Parameters:

  • list
  • item
set (list)
Creates a set (a map of keys to true) from the list supplied.

Parameters:

  • list
slice (list, istart, iend)
Slice a list.

Parameters:

  • list
  • istart
  • iend

Returns:

    the sublist of elements at indices [istart, iend) of the supplied list.
sort (t, f)
A wrapper on table.sort that uses locale-insensitive comparison for strings by default. Like table.sort, this sorts an array in-place.

Parameters:

  • t array an array to sort.
  • f function(a,b) an optional less-than function to use for determining sort order.
stable_lessthan (x1, x2)
a locale-insensitive less-than operation.

Parameters:

  • x1
  • x2
subclass (parent, subclassname)
Define a subclass

Parameters:

  • parent should have no-arg constructor.
  • subclassname name
trim (s)
Remove leading and trailing whitespace From http://lua-users.org/wiki/CommonFunctions

Parameters:

  • s

See also:

values (map)
Returns a list of the values in the given map.

Parameters:

  • map
generated by LDoc 1.4.6 Last updated 2021-01-09 22:46:05