Module pl.func
Functional helpers like composition, binding and placeholder expressions.
Placeholder expressions are useful for short anonymous functions, and were inspired by the Boost Lambda library.
> utils.import 'pl.func' > ls = List{10,20,30} > = ls:map(_1+1) {11,21,31}
They can also be used to bind particular arguments of a function.
> p = bind(print,'start>',_0) > p(10,20,30) > start> 10 20 30
See the Guide
Dependencies: pl.utils, pl.tablex
Functions
import (tname, context) | wrap a table of functions. |
register (fun[, name]) | register a function for use in placeholder expressions. |
tail (ls) | all elements of a table except the first. |
repr (e, lastpred) | create a string representation of a placeholder expression. |
instantiate (e) | instantiate a PE into an actual function. |
I (e) | instantiate a PE unless it has already been done. |
bind1 (fn, p) | bind the first parameter of the function to a value. |
compose (f, g) | create a function which chains two functions. |
bind (fn, ...) | bind the arguments of a function to given values. |
Functions
- import (tname, context)
-
wrap a table of functions. This makes them available for use in
placeholder expressions.
Parameters:
- tname string a table name
- context tab context to put results, defaults to environment of caller
- register (fun[, name])
-
register a function for use in placeholder expressions.
Parameters:
- fun func a function
- name string an optional name (optional)
Returns:
-
a placeholder functiond
- tail (ls)
-
all elements of a table except the first.
Parameters:
- ls tab a list-like table.
- repr (e, lastpred)
-
create a string representation of a placeholder expression.
Parameters:
- e a placeholder expression
- lastpred not used
- 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
Returns:
-
a function
- I (e)
-
instantiate a PE unless it has already been done.
Parameters:
- e a placeholder expression
Returns:
-
the function
- bind1 (fn, p)
-
bind the first parameter of the function to a value.
Parameters:
- fn func a function of one or more arguments
- p a value
Returns:
-
a function of one less argument
Usage:
(bind1(math.max,10))(20) == math.max(10,20)
- compose (f, g)
-
create a function which chains two functions.
Parameters:
- f func a function of at least one argument
- g func a function of at least one argument
Returns:
-
a function
Usage:
printf = compose(io.write,string.format)
- bind (fn, ...)
-
bind the arguments of a function to given values.
bind(fn,v,_2)
is equivalent tobind1(fn,v)
.Parameters:
- fn func a function of at least one argument
- ... values or placeholder variables
Returns:
-
a function
Usage:
(bind(f,_1,a))(b) == f(a,b)
(bind(f,_2,_1))(a,b) == f(b,a)