Module utils

General utility functions.

Note: This module is not part of public API!

Functions

always (value) Returns a function that always returns the given value.
any (func, list) Returns true if at least one of elements of the list match the predicate, false otherwise.
check_args (type_specs, ...) Checks that the given arguments have the correct type.
dedup (list) Removes repeated elements from the given list (in-place).
default (default, value) Returns value if it's not nil, otherwise returns default.
ends_with (suffix, str) Returns true if the string str ends with the suffix.
errorf (message, ...) The same as error, but with formatted message.
filter (predicate, list) Returns a new list containing the items in the given list for which the predicate function does not return false or nil.
find (predicate, list) Searches through the list and returns the first value that passes the predicate function.
is_empty (value) Returns true if the value is nil or empty string.
index_map (list) Create an index map from the list.
last (list) Returns the last element of the list.
merge (...) Returns a new table containing the contents of all the given tables.
partial (func, ...) Partial application.
printf (str, ...) Prints the given string to the stdout, ended with a newline.
push_all (dest, src) Inserts items from the src list at the end of the dest list and returns modified dest (i.e.
reject (predicate, list) Returns a new list containing the items in the given list for which the predicate function returns false or nil.
remove_shebang (chunk) Returns copy of the given chunk without shebang.
shallow_clone (tab) Makes a shallow clone of the given table.
size (tab) Counts total number of elements in the given table; both in the array part and the hash part.
split (delim, str) Splits the given str into a list of strings based on the delimiter delim.
starts_with (prefix, str) Returns true if the string str starts with the prefix.
tableize (value) Wraps the given value to a table, or returns as-is if it is table.

Fields

LUA_VERSION Version of the Lua interpreter running this code in format x.y.


Functions

always (value)
Returns a function that always returns the given value.

Parameters:

  • value

Returns:

    function () -> value
any (func, list)
Returns true if at least one of elements of the list match the predicate, false otherwise.

Parameters:

  • func function The predicate function.
  • list table The list to test.

Returns:

    bool Whether the predicate is satisfied by at least one element.
check_args (type_specs, ...)
Checks that the given arguments have the correct type.

Parameters:

  • type_specs string A colon separated list of allowed types for each argument. Multiple allowed types per argument are separated by | (without spaces).
  • ... Arguments to check against type_specs.

Raises:

on type mismatch.

Usage:

    check_args('table, string|table, ?table', a, b, c)
dedup (list)
Removes repeated elements from the given list (in-place).

Parameters:

  • list table The list to deduplicate.

Returns:

    table A sorted list of unique elements.
default (default, value)
Returns value if it's not nil, otherwise returns default.

Parameters:

  • default
  • value
ends_with (suffix, str)
Returns true if the string str ends with the suffix.

Parameters:

Returns:

    bool
errorf (message, ...)
The same as error, but with formatted message.

Parameters:

  • message string The error message.
  • ... Arguments for string.format being applied to the message.
filter (predicate, list)
Returns a new list containing the items in the given list for which the predicate function does not return false or nil.

Parameters:

  • predicate function
  • list table

Returns:

    table
find (predicate, list)
Searches through the list and returns the first value that passes the predicate function.

Parameters:

  • predicate function
  • list table

Returns:

    The first list's value passing predicate.
is_empty (value)
Returns true if the value is nil or empty string.

Parameters:

  • value

Returns:

    bool
index_map (list)
Create an index map from the list. The original values become keys, and the associated values are the indices into the original list.

Parameters:

Returns:

    table
last (list)
Returns the last element of the list.

Parameters:

Returns:

    Last element of the list.
merge (...)
Returns a new table containing the contents of all the given tables. Tables are iterated using pairs, so this function is intended for tables that represent associative arrays. Entries with duplicate keys are overwritten with the values from a later table.

Parameters:

Returns:

    table A new table.
partial (func, ...)
Partial application. Takes a function func and arguments, and returns a function func2. When applied, func2 returns the result of applying func to the arguments provided initially followed by the arguments provided to func2.

Parameters:

  • func function
  • ... Arguments to pass to the func.

Returns:

    function A partially applied function.
printf (str, ...)
Prints the given string to the stdout, ended with a newline.

Parameters:

push_all (dest, src)
Inserts items from the src list at the end of the dest list and returns modified dest (i.e. it modifies it in-place!).

Parameters:

  • dest table The destination list to extend.
  • src table The source list to take items from.

Returns:

    table The given dest list.
reject (predicate, list)
Returns a new list containing the items in the given list for which the predicate function returns false or nil.

Parameters:

  • predicate function
  • list table

Returns:

    table
remove_shebang (chunk)
Returns copy of the given chunk without shebang.

Parameters:

Returns:

    string
shallow_clone (tab)
Makes a shallow clone of the given table.

Parameters:

Returns:

    table
size (tab)
Counts total number of elements in the given table; both in the array part and the hash part.

Parameters:

  • tab table The table to count size of.

Returns:

    int A total number of elements.
split (delim, str)
Splits the given str into a list of strings based on the delimiter delim. If the str is falsy, then empty table is returned.

Parameters:

  • delim string The delimiter pattern.
  • str string The string to split.

Returns:

    {string,...}
starts_with (prefix, str)
Returns true if the string str starts with the prefix.

Parameters:

Returns:

    bool
tableize (value)
Wraps the given value to a table, or returns as-is if it is table.

Parameters:

  • value The value to wrap.

Returns:

    table A table.

Fields

LUA_VERSION
Version of the Lua interpreter running this code in format x.y.
generated by LDoc 1.4.6