/**
* Some utilities for working with functions that are not yet implemented.
*/
public class FunctionUtils {
/**
* Given unary functions f and g, build the unary function f o g. That
* is, build the function (lambda (x) (f (g x))).
* ...
*/
public static UnaryFunction compose(final UnaryFunction f,
final UnaryFunction g) {
return new UnaryFunction() {
public C apply(A arg) throws Exception {
return f.apply(g.apply(arg));
} // apply
}; // new UnaryFunction
} // compose(UnaryFunction, UnaryFunction)
/**
* Given unary function f and binary function g, build the binary function
* f o g. That is, build the function (lambda (x y) (f (g x y))).
*/
public static BinaryFunction compose(final UnaryFunction f,
final BinaryFunction g) {
return new BinaryFunction() {
public D apply(A left, B right) throws Exception {
return f.apply(g.apply(left, right));
} // apply
}; // new BinaryFunction
} // compose(Function)
/**
* Given a binary function, f, and two unary functions, g and h, build the
* function (lambda (x,y) (f (g x) (h y)))
*/
public static BinaryFunction compose(final BinaryFunction f,
final UnaryFunction g, final UnaryFunction h) {
return new BinaryFunction() {
public E apply(A left, C right) throws Exception {
return f.apply(g.apply(left), h.apply(right));
} // apply
}; // new BinaryFunction
} // compose(Function)
/**
* Build a new function by filling in the left argument of f.
*/
public static UnaryFunction leftSection(
final BinaryFunction f, final A left) {
return new UnaryFunction() {
public C apply(B arg) throws Exception {
return f.apply(left, arg);
} // apply
}; // new UnaryFunction
} // leftSection
/**
* Build a new function by filling in the right argument of f.
*/
public static UnaryFunction rightSection(
final BinaryFunction f, final B right) {
return new UnaryFunction() {
public C apply(A arg) throws Exception {
return f.apply(arg, right);
} // apply
}; // new UnaryFunction
} // rightSection
/**
* Map a unary function over an array.
*
* @post
* For each i, 0 <= i < vals.length
* results[i] = fun.apply(vals[i])
*/
@SuppressWarnings("unchecked")
public static Output[] map(UnaryFunction fun, Input[] vals) throws Exception {
Output[] results = (Output[]) new Object[vals.length];
for (int i = 0; i < vals.length; i++) {
results[i] = fun.apply(vals[i]);
} // for
return results;
} // map
/**
* Map a binary function over two arrays.
*
* @pre
* left.length == right.length
* @post
* For each i, 0 <= i < vals.length
* results[i] = fun.apply(left[i], right[i])
*/
@SuppressWarnings("unchecked")
public static Output[] map(BinaryFunction fun, Left[] left,
Right[] right) throws Exception {
Output[] results = (Output[]) new Object[left.length];
for (int i = 0; i < left.length; i++) {
results[i] = fun.apply(left[i], right[i]);
} // for
return results;
} // map
} // class FunctionUtils