Module pl.utils

Generally useful routines.

Functions

args (...) take an arbitrary set of arguments and make into a table.
bind1 (fn, p) bind the first argument of the function to a value.
choose (cond, value1, value2) return either of two values, depending on a condition.
escape (s) escape any 'magic' characters in a string
fprintf (f, fmt, ...) write an arbitrary number of arguments to a file using a format.
function_arg (f) process a function argument.
import (t, T) take a table and 'inject' it into the local namespace.
is_callable (obj) is the object either a function or a callable object?.
is_type (obj, tp) is the object of the specified type?.
memoize (func) 'memoize' a function (cache returned value for next call).
printf (fmt, ...) print an arbitrary number of arguments using a format.
quit (code, msg, ...) end this program gracefully.
readfile (filename) return the contents of a file as a string
readlines (filename) return the contents of a file as a list of lines
split (s, re) split a string into a list of strings separated by a delimiter.
splitl (s) split a string into a list of strings separated by either spaces or commas.
splitv (s, re) split a string into a number of values.
writefile (filename, str) write a string to a file


Functions

args (...)
take an arbitrary set of arguments and make into a table. This returns the table and the size; works fine for nil arguments

Parameters:

  • ...: arguments

Usage:

    local t,n = utils.args(...)

Return values:

  1. table
  2. table size
bind1 (fn, p)
bind the first argument of the function to a value.

Parameters:

  • fn: a function of at least two values
  • p: a value

Return value:

    a function such that f(x) is fn(p,x)

See also:

choose (cond, value1, value2)
return either of two values, depending on a condition.

Parameters:

  • cond: A condition
  • value1: Value returned if cond is true
  • value2: Value returned if cond is false (can be optional)
escape (s)
escape any 'magic' characters in a string

Parameters:

  • s: The input string
fprintf (f, fmt, ...)
write an arbitrary number of arguments to a file using a format.

Parameters:

  • f:
  • fmt: The format (see string.format)
  • ...:
function_arg (f)
process a function argument.
This is used throughout Penlight and defines what is meant by a function:
Something that is_callable, or an operator string as defined by pl.operator,
such as '>' or '#'.

Parameters:

  • f: a function, operator string, or callable object

Return value:

    a callable
import (t, T)
take a table and 'inject' it into the local namespace.

Parameters:

  • t: The Table
  • T: An optional destination table (defaults to callers environment)
is_callable (obj)
is the object either a function or a callable object?.

Parameters:

  • obj:
is_type (obj, tp)
is the object of the specified type?. If the type is a string, then use type, otherwise compare with metatable

Parameters:

  • obj: an object
  • tp: a type
memoize (func)
'memoize' a function (cache returned value for next call). This is useful if you have a function which is relatively expensive, but you don't know in advance what values will be required, so building a table upfront is wasteful/impossible.

Parameters:

  • func: a function of at least one argument

Return value:

    a function with at least one argument, which is used as the key.
printf (fmt, ...)
print an arbitrary number of arguments using a format.

Parameters:

  • fmt: The format (see string.format)
  • ...:
quit (code, msg, ...)
end this program gracefully.

Parameters:

  • code: The exit code
  • msg: A message to be printed
  • ...: extra arguments for fprintf

See also:

readfile (filename)
return the contents of a file as a string

Parameters:

  • filename: The file path

Return value:

    file contents
readlines (filename)
return the contents of a file as a list of lines

Parameters:

  • filename: The file path

Return value:

    file contents as a table
split (s, re)
split a string into a list of strings separated by a delimiter.

Parameters:

  • s: The input string
  • re: A regular expression; defaults to spaces

Return value:

    a list-like table
splitl (s)
split a string into a list of strings separated by either spaces or commas.

Parameters:

  • s: The input string

Return value:

    a list-like table
splitv (s, re)
split a string into a number of values.

Parameters:

  • s: the string
  • re: the delimiter, default space

Usage:

    first,next = splitv('hello:dolly',':')

Return value:

    n values

See also:

writefile (filename, str)
write a string to a file

Parameters:

  • filename: The file path
  • str: The string

Valid XHTML 1.0!