de.cm.frw.core.fun.impl
Class PF

java.lang.Object
  extended by de.cm.frw.core.fun.impl.Function
      extended by de.cm.frw.core.fun.impl.PF

public class PF
extends Function

PF is a name space for static components in support of pure functional programming. These operate mainly on functions to create new functions.

The concepts are implemented as static member Function instances.

Example O: Composition

The static method PF.compose takes two function instance f and g and returs a new function

 c = f.g with c(args) = f(g(args))
 
The composition is implemented as a function instance of two arguments (functions f and g) that produces a new function (c)

Example I: Reduction

While it is possible to use Function.reduce to perform a reduction by reducing an Iterable through repeated application of a given function, the PF.reduce method takes a function and an initial value to produce a new function, that can be applied to an Iterable.

To achieve this the PF.reduce method uses the Function instance reduce, i.e. the reduction is implemented as a function and can itself be used in higher order concepts.

Author:
cm

Nested Class Summary
 
Nested classes/interfaces inherited from class de.cm.frw.core.fun.impl.Function
Function.InvalidArguments
 
Field Summary
static Function adapt
          adapt is a function of three arguments f : a function of n arguments i : an integer with 0 <= i < n g : a function of one argument that produces a new function a of n arguments that computes its result by applying f to its arguments using g(x) in parameter position i:
static Function bind
          bind is a function of three arguments f : a function of n arguments i : an integer with 0 <= i < n x : an Object that produces a new function b of n - 1 arguments that computes its result by applying f to its n - 1 arguments using x in parameter position i:
static Function compose
          compose is a function of two arguments f : a function of one argument g : a function of n arguments that produces an new function c of n arguments (i.e.
static Function map
           
static Function reduce
          reduce is a function of two arguments f : a function of two arguments, init : an object that produces a new Function R of one arguments I : an Iterable instance { i_1, ..., i_n } as follows:
static Function unary
          unary is a function of one argument f : a function of n parameters that produces a new function U of one argument x : an object that puts its single argument x in all parameter positions of f, hence
 
Constructor Summary
PF(Function f)
           
 
Method Summary
static Function adapt(Function f, int i, Function a)
           
 PF adapt(int pos, Function a)
           
 java.lang.Object apply()
           
 java.lang.Object apply(java.util.Collection args)
          Applies the implemented function to the passed in collection of arguments.
 java.lang.Object apply(java.lang.Object arg)
           
 int arity()
           
static Function bind(Function f, int i, java.lang.Object arg)
           
 PF bind(int pos, java.lang.Object val)
           
 void checkArguments(java.util.Collection args)
          Override this method to check conditions on the arguments (like types) passed to the function in apply before the operate method is called.
 PF compose(Function g)
           
static Function compose(Function f, Function g)
           
 java.lang.Iterable map(java.util.Collection args)
           
static Function map(Function f)
           
 java.lang.Object operate(java.lang.Object[] operands)
          Override this to implement the actual operation realized by this function.
static Function reduce(Function f, java.lang.Object init)
           
 PF unary()
           
static Function unary(Function f)
           
 
Methods inherited from class de.cm.frw.core.fun.impl.Function
apply, apply, apply, apply, apply, apply, apply, apply, apply, apply, apply, apply, apply, apply, apply, box, box, box, box, box, box, box, box, box, box, box, box, box, box, box, is, main, map, reduce, unbox, val
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

compose

public static final Function compose
compose is a function of two arguments
  1. f : a function of one argument
  2. g : a function of n arguments
that produces an new function c of n arguments (i.e. same arity as g) which applies f to the result of applying g to its n arguments:

compose(f, g) = c with c(x1, ... , xn) = f(g(x1, ... , xn))

Note: this is sometimes written as c = f . g


bind

public static final Function bind
bind is a function of three arguments
  1. f : a function of n arguments
  2. i : an integer with 0 <= i < n
  3. x : an Object
that produces a new function b of n - 1 arguments that computes its result by applying f to its n - 1 arguments using x in parameter position i:

bind(f, i, x) = b with b(x1, ... , xn-1) = f(x1, .., xi-1, x , xi+1, .. , xn-1)

Note: parameter positions are counted from 0


adapt

public static final Function adapt
adapt is a function of three arguments
  1. f : a function of n arguments
  2. i : an integer with 0 <= i < n
  3. g : a function of one argument
that produces a new function a of n arguments that computes its result by applying f to its arguments using g(x) in parameter position i:

adapt(f, i, g) = a with a(x1, ... , xn) = f(x1, .., g(xi), .. , xn)

Note: parameter positions are counted from 0


unary

public static final Function unary
unary is a function of one argument
  1. f : a function of n parameters
that produces a new function U of one argument
  1. x : an object
that puts its single argument x in all parameter positions of f, hence

unary(f) = U with U(x) = f(x, x, ... , x)


reduce

public static final Function reduce
reduce is a function of two arguments
  1. f : a function of two arguments,
  2. init : an object
that produces a new Function R of one arguments
  1. I : an Iterable instance { i_1, ..., i_n }
as follows:

reduce(f, init) = R with R(I) = R(i_1 ... i_n) = f(i_n, f(i_n-1, ... , f(i_1, init)))


map

public static final Function map
Constructor Detail

PF

public PF(Function f)
Method Detail

compose

public static Function compose(Function f,
                               Function g)

bind

public static Function bind(Function f,
                            int i,
                            java.lang.Object arg)

adapt

public static Function adapt(Function f,
                             int i,
                             Function a)
                      throws Function.InvalidArguments
Throws:
Function.InvalidArguments

unary

public static Function unary(Function f)
                      throws Function.InvalidArguments
Throws:
Function.InvalidArguments

reduce

public static Function reduce(Function f,
                              java.lang.Object init)
                       throws Function.InvalidArguments
Throws:
Function.InvalidArguments

map

public static Function map(Function f)
                    throws Function.InvalidArguments
Throws:
Function.InvalidArguments

bind

public PF bind(int pos,
               java.lang.Object val)
        throws Function.InvalidArguments
Throws:
Function.InvalidArguments

adapt

public PF adapt(int pos,
                Function a)
         throws Function.InvalidArguments
Throws:
Function.InvalidArguments

compose

public PF compose(Function g)
           throws Function.InvalidArguments
Throws:
Function.InvalidArguments

unary

public PF unary()
         throws Function.InvalidArguments
Throws:
Function.InvalidArguments

apply

public java.lang.Object apply(java.util.Collection args)
                       throws Function.InvalidArguments
Description copied from class: Function
Applies the implemented function to the passed in collection of arguments. Checks number of arguments against arity (args.size() < arity ==> fail) and calls checkArguments to perform checks as implemented there.

Overrides:
apply in class Function
Parameters:
args - A collection of arguments
Returns:
The function result.
Throws:
Function.InvalidArguments

checkArguments

public void checkArguments(java.util.Collection args)
                    throws Function.InvalidArguments
Description copied from class: Function
Override this method to check conditions on the arguments (like types) passed to the function in apply before the operate method is called. Throw the declared exception with a meaningful message when condition are not met. Note: Number of arguments < arity is checked before this is called to have enough arguments to operate on, however if more is no good, check it here ... Default implementation does nothing.

Overrides:
checkArguments in class Function
Throws:
Function.InvalidArguments

operate

public java.lang.Object operate(java.lang.Object[] operands)
                         throws Function.InvalidArguments
Description copied from class: Function
Override this to implement the actual operation realized by this function.

Remember to document the number and types of the operands and any other conditions for running the operate method. Or implement checkArguments instead.

Default implementation returns first argument passed in, realizing sort of an unary identity function.

Overrides:
operate in class Function
Parameters:
operands - An array of the arguments that where passed to apply
Returns:
An object as a result of the operation
Throws:
Function.InvalidArguments - when you implement it to do so ...

apply

public java.lang.Object apply()
                       throws Function.InvalidArguments
Overrides:
apply in class Function
Throws:
Function.InvalidArguments

apply

public java.lang.Object apply(java.lang.Object arg)
                       throws Function.InvalidArguments
Overrides:
apply in class Function
Throws:
Function.InvalidArguments

map

public java.lang.Iterable map(java.util.Collection args)
                       throws Function.InvalidArguments
Overrides:
map in class Function
Throws:
Function.InvalidArguments

arity

public int arity()
Overrides:
arity in class Function