Module pl.func
Functional helpers like composition,binding and placeholder expressions.
Functions
I (e) | instantiate a PE unless it has already been done. |
bind (fn, ...) | bind the arguments of a function to given values. |
compose (f, g) | create a function which chains two functions. |
curry (fn, p) | bind the first parameter of the function to a value. |
import (tname, context) | wrap a table of functions. |
instantiate (e) | instantiate a PE into an actual function. |
register (fun, name) | register a function for use in placeholder expressions. |
repr (e, lastpred) | create a string representation of a placeholder expression. |
tail (ls) | all elements of a table except the first. |
Functions
- I (e)
-
instantiate a PE unless it has already been done.
Parameters:
-
e
: a placeholder expression
Return value:
- the function
-
- bind (fn, ...)
-
bind the arguments of a function to given values. bind(fn,v,_2) is equivalent to curry(fn,v).
Parameters:
-
fn
: a function of at least one argument -
...
: values or placeholder variables
Usage:
(bind(f,_1,a))(b) == f(a,b)
(bind(f,_2,_1))(a,b) == f(b,a)
Return value:
- a function
-
- compose (f, g)
-
create a function which chains two functions.
Parameters:
-
f
: a function of at least one argument -
g
: a function of at least one argument
Usage:
printf = compose(io.write,string.format)
Return value:
- a function
-
- curry (fn, p)
-
bind the first parameter of the function to a value.
Parameters:
-
fn
: a function of one or more arguments -
p
: a value
Usage:
(curry(math.max,10))(20) == math.max(10,20)
Return value:
- a function of one less argument
-
- import (tname, context)
-
wrap a table of functions. This makes them available for use in placeholder expressions.
Parameters:
-
tname
: a table name -
context
: context to put results, defaults to local environment
-
- instantiate (e)
-
instantiate a PE into an actual function. First we find the largest placeholder used, e.g. _2; from this a list of the formal parameters can be build. Then we collect and replace any non-PE values from the PE, and build up a constant binding list. Finally, the expression can be compiled, and e.__PE_function is set.
Parameters:
-
e
: a placeholder expression
Return value:
- a function
-
- register (fun, name)
-
register a function for use in placeholder expressions.
Parameters:
-
fun
: a function -
name
:
Return value:
- a placeholder functiond
-
- repr (e, lastpred)
-
create a string representation of a placeholder expression.
Parameters:
-
e
: a placeholder expression -
lastpred
:
-
- tail (ls)
-
all elements of a table except the first.
Parameters:
-
ls
: a list-like table.
-