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

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.
curry (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: a table name
  • context: context to put results, defaults to environment of caller
register (fun, name)
register a function for use in placeholder expressions.

Parameters:

  • fun: a function
  • name: an optional name

Returns:

    a placeholder functiond
tail (ls)
all elements of a table except the first.

Parameters:

  • ls: 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
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)

Returns:

    a function of one less argument
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)

Returns:

    a 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)

Returns:

    a function
generated by LDoc 1.2