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 multiple 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:
- register (fun[, name])
-
register a function for use in placeholder expressions.
Parameters:
- fun function a function
- name string an optional name (optional)
Returns:
-
a placeholder functiond
- tail (ls)
-
all elements of a table except the first.
Parameters:
- ls table 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.PEfunction 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 function 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 multiple functions.
Parameters:
- f function a function of at least one argument
- g function a function of at least one argument
- ... additional functions to compose
Returns:
-
a function
Usage:
printf = compose(io.write, string.format)
printf = compose(io.write, string.lower, string.format)
- bind (fn, ...)
-
bind the arguments of a function to given values.
bind(fn,v,_2)
is equivalent tobind1(fn,v)
.Parameters:
- fn function 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)