base

Adds to the existing global functions


require "list" FIXME: sort out op table


require "io_ext" FIXME: allow loops


metamethod: Return given metamethod, if any, or nil
  x: object to get metamethod of
  n: name of metamethod to get
returns
  m: metamethod function or nil if no metamethod or not a
    function


render: Turn tables into strings with recursion detection
N.B. Functions calling render should not recurse, or recursion
detection will not work
  x: object to convert to string
  open: open table renderer
    @t: table
  returns
    @s: open table string
  close: close table renderer
    @t: table
  returns
    @s: close table string
  elem: element renderer
    @e: element
  returns
    @s: element string
  pair: pair renderer
    N.B. this function should not try to render i and v, or treat
    them recursively
    @t: table
    @i: index
    @v: value
    @is: index string
    @vs: value string
  returns
    @s: element string
  sep: separator renderer
    @t: table
    @i: preceding index (nil on first call)
    @v: preceding value (nil on first call)
    @j: following index (nil on last call)
    @w: following value (nil on last call)
  returns
    @s: separator string
returns
  s: string representation


tostring: Extend tostring to work better on tables
  x: object to convert to string
returns
  s: string representation


print: Make print use tostring, so that improvements to tostring
are picked up
  arg: objects to print


prettytostring: pretty-print a table
  @t: table to print
  @indent: indent between levels ["\t"]
  @spacing: space before every line
returns
  @s: pretty-printed string


totable: Turn an object into a table according to __totable
metamethod
  x: object to turn into a table
returns
  t: table or nil


pickle: Convert a value to a string
The string can be passed to dostring to retrieve the value
TODO: Make it work for recursive tables
  x: object to pickle
returns
  s: string such that eval (s) is the same value as x


id: Identity
  @param ...
returns
  ...: the arguments passed to the function


pack: Turn a tuple into a list
  ...: tuple
returns
  l: list


bind: Partially apply a function
  f: function to apply partially
  a1 ... an: arguments to bind
returns
  g: function with ai already bound


curry: Curry a function
  f: function to curry
  n: number of arguments
returns
  g: curried version of f


compose: Compose functions
  f1 ... fn: functions to compose
returns
  g: composition of f1 ... fn
    args: arguments
  returns
    @param f1 (...fn (args)...)


eval: Evaluate a string
  s: string
returns
  v: value of string


ripairs: An iterator like ipairs, but in reverse
  t: table to iterate over
returns
  f: iterator function
    t: table
    n: index
  returns
    i: index (n - 1)
    v: value (t[n - 1])
  t: the table, as above
  n: #t + 1


collect: collect the results of an iterator
  i: iterator
  ...: arguments
returns
  @t: results of running the iterator on its arguments


map: Map a function over an iterator
  f: function
  i: iterator
  ...: iterator's arguments
returns
  t: result table


filter: Filter an iterator with a predicate
  p: predicate
  i: iterator
  ...:
returns
  t: result table containing elements e for which p (e)


fold: Fold a function into an iterator leftwards
  f: function
  d: element to place in left-most position
  i: iterator
  ...:
returns
  r: result


treeIter: tree iterator
  t: tree to iterate over
returns
  f: iterator function
  returns
    e: event
    t: table of values


FIXME: this version is more obvious but has an illegal yield
treeIter: tree iterator
  t: tree to iterate over
returns
  f: iterator function
  returns
    e: event
    t: table of values
function _G.treeIter (t)
  if not coroutine.yield ("branch", t) then
    for i, v in ipairs (t) do
      if type (v) ~= "table" then
        if coroutine.yield ("leaf", {i, v}) then
          break
        end
      else
        f (v)
      end
    end
    coroutine.yield ("join", t)
  end
end


assert: Extend to allow formatted arguments
  v: value
  f, ...: arguments to format
returns
  v: value


warn: Give warning with the name of program and file (if any)
  ...: arguments for format


die: Die with error
  ...: arguments for format


Function forms of operators