# base

Adds to the existing global functions

Functional forms of infix operators
Defined here so that other modules can write to it.

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

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

nodes: tree iterator
tr: tree to iterate over
returns
f: iterator function
n: current node
p: path to node within the tree
@yields
ty: type ("leaf", "branch" (pre-order) or "join" (post-order))
p_: path to node ({i1...ik})
n_: node

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 binary function into an iterator
f: function
d: initial first argument
i: iterator
...:
returns
r: result

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