org.fun4j
Class Template

java.lang.Object
  extended by org.fun4j.Template

public class Template
extends java.lang.Object

This Class is the central facade to fun4j. It provides access to most components that fun4j users will use in their code.

Author:
Thomas Mahler

Field Summary
static Template fun4j
          by using a static import this instance can be made available in user code.
 Function identity
          the identity Function identity(x) = x
 
Constructor Summary
Template()
           
 
Method Summary
<T> T
$(java.lang.Class<T> outerInterface, java.lang.String methodBody)
           
 java.util.Collection asCollection(Cons cons)
          This utility method converts a Lisp list (i.e. a Cons object) into a Collection.
<E> java.util.Collection<E>
asCollection(E... elements)
          this utility method creates a Collection from a comma separated list of elements
<E> Cons
asCons(java.util.Collection<E> col)
          this utility method converts a Collection into a Lisp list (i.e. a Cons object)
 Function bind(Function fun, java.lang.Object... args)
          binds parameters to the variables of a function.
 Function compile(Expression exp, java.lang.String name)
          compiles an AST Expression into an executable Java Function.
 Function compile(java.lang.String lispTerm)
          compiles a LISP term into an executable Java Function.
 Function compose(Function... functions)
          composes a set of functions functions[1..n] to a new function functions[1] o functions[2] o ... functions[n]
 Function compose(Function f, Function g)
          composes two functions f(x) and g(x) to f(g(x)).
 Function constant(java.lang.Object obj)
          produces a Function that returns a constant value.
 void define(java.lang.String name, java.lang.Object value)
          binds name to value in the global Lisp environment
 java.lang.Object eval(java.lang.String lispTerm, java.lang.Object... args)
          evaluates a LISP term.
<E> boolean
exists(Function predicate, java.util.Collection<E> collection)
          if the Function predicate evaluates to true for any element of the input collection true is returned, else false
<E> java.util.Collection<E>
filter(Function predicate, java.util.Collection<E> collection)
          all elements of the input collection that match the predicate are collected into a new Collection.
static Function fn(java.lang.String lambdaTerm)
          by using a static import on this method you can define Lisp functions in user code as shown in the following example: import static org.fun4j.Template.fn; Function add = fn("(lambda (x y) (+ x y))");
<E,F> F
foldleft(Function fun, F acc, java.util.Collection<E> col)
          performs a foldleft operation of Function fun over the Collection col.
<E,F> F
foldleft(Function fun, F acc, java.util.Iterator<E> iter)
          performs a foldleft operation of Function fun over Iterator iter.
<E,F> F
foldright(Function fun, F acc, java.util.Collection<E> col)
          performs a foldright of Function fun over a Collection.
<E,F> F
foldright(Function fun, F acc, java.util.Iterator<E> iter)
          performs a foldright of Function fun over Iterator iter.
<E> boolean
forAll(Function predicate, java.util.Collection<E> collection)
          if the Function predicate evaluates to true for all elements in the input collection, true is returned, else false.
<E,F> java.util.Collection<F>
map(Function fun, java.util.Collection<E> col)
          applies the Function fun to each element of the Collection col.
<E,F> java.util.Collection<F>
map(Function fun, java.util.Iterator<E> iter)
          applies the Function fun to each element of the Iterator iter.
 java.lang.Object parse(java.lang.String lispTerm)
          parses a Lisp term input into its AST representation.
 Expression precompile(java.lang.String lispTerm)
          precompiles a LISP term into an AST Expression.
 void runFile(java.lang.String filename)
          evaluates all lisp expression in a file.
 boolean setTracing(boolean value)
          if set to true tracing of Lisp function calls is traced.
 boolean setUseBigInts(boolean newValue)
          if set to true (the default value) the Parser and Compiler will work with BigIntegers.
 boolean setWriteFile(boolean newValue)
          if set to true the Compiler will write precompiled expression into tmp/*.lsp files, compiled bytecote into tmp/*.class files and debugging information is generated into the bytecode.
 Function toFunction(java.lang.reflect.Method method)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fun4j

public static Template fun4j
by using a static import this instance can be made available in user code. Here an example: import static org.fun4j.Template.fun4j; fun4j.eval("(fac 10)");


identity

public Function identity
the identity Function identity(x) = x

Constructor Detail

Template

public Template()
Method Detail

fn

public static Function fn(java.lang.String lambdaTerm)
                   throws CompilationException
by using a static import on this method you can define Lisp functions in user code as shown in the following example:
 import static org.fun4j.Template.fn;
 
 Function add = fn("(lambda (x y) (+ x y))");
 

Parameters:
lambdaTerm - the lambdaTerm defining the function.
Returns:
the compiled Function object. The function can be executed by calling Function.apply(Object...).
Throws:
CompilationException

parse

public java.lang.Object parse(java.lang.String lispTerm)
parses a Lisp term input into its AST representation. nil is parsed to null T or true is parsed to boolean true F or false is parsed to boolean true Numbers are parsed to Integers / BigIntegers or BigDecimals Symbols and "quoted strings" are parsed to Strings Lists are parsed into Cons Binary trees The Readmacro 'x is expanded to (QUOTE x)

Parameters:
lispTerm -
Returns:
the AST representation

compile

public Function compile(Expression exp,
                        java.lang.String name)
                 throws CompilationException
compiles an AST Expression into an executable Java Function.

Parameters:
exp - the Expression to compile
name - the Name of this expression, will be used for creation of the internal Class-Name
Returns:
the compiled Function
Throws:
CompilationException

compile

public Function compile(java.lang.String lispTerm)
                 throws CompilationException
compiles a LISP term into an executable Java Function.

Parameters:
lispTerm - the LISP term to be compiled
Returns:
the compiled Function
Throws:
CompilationException

precompile

public Expression precompile(java.lang.String lispTerm)
                      throws CompilationException
precompiles a LISP term into an AST Expression. This expression can be compiled to executable Java code by calling compile(Expression, String)

Parameters:
lispTerm - the LISP term to be precompiled
Returns:
the Expression representing the LISP term
Throws:
CompilationException

bind

public Function bind(Function fun,
                     java.lang.Object... args)
binds parameters to the variables of a function. Binding will return a PartialApplication containing function and arguments.

Parameters:
fun - the function
args - the parameter to be bound to the function
Returns:
a PartialApplication containing function and arguments.

eval

public java.lang.Object eval(java.lang.String lispTerm,
                             java.lang.Object... args)
                      throws CompilationException
evaluates a LISP term.

Parameters:
lispTerm - the term to evaluate
args - optional parameters are passed in as function arguments
Returns:
the result of the evaluation
Throws:
CompilationException

runFile

public void runFile(java.lang.String filename)
evaluates all lisp expression in a file.

Parameters:
filename - the file to execute.

define

public void define(java.lang.String name,
                   java.lang.Object value)
binds name to value in the global Lisp environment

Parameters:
name - the name to be used
value - the value to be associated with the name.

setTracing

public boolean setTracing(boolean value)
if set to true tracing of Lisp function calls is traced. If set to false it's disabled.

Parameters:
value - the new value
Returns:
the old value

setUseBigInts

public boolean setUseBigInts(boolean newValue)
if set to true (the default value) the Parser and Compiler will work with BigIntegers. If set to false they will use Integers.

Parameters:
newValue - the new value
Returns:
the old value

setWriteFile

public boolean setWriteFile(boolean newValue)
if set to true the Compiler will write precompiled expression into tmp/*.lsp files, compiled bytecote into tmp/*.class files and debugging information is generated into the bytecode.

Parameters:
newValue - the new value
Returns:
the old value

asCollection

public <E> java.util.Collection<E> asCollection(E... elements)
this utility method creates a Collection from a comma separated list of elements

Parameters:
elements - Comma separated list of elements
Returns:
the resulting Collection

asCollection

public java.util.Collection asCollection(Cons cons)
This utility method converts a Lisp list (i.e. a Cons object) into a Collection.

Parameters:
cons - the Lisp list
Returns:
the resulting Collection

asCons

public <E> Cons asCons(java.util.Collection<E> col)
this utility method converts a Collection into a Lisp list (i.e. a Cons object)

Type Parameters:
E - the element type of the Collection
Parameters:
col - the collection
Returns:
the resulting Cons object

map

public <E,F> java.util.Collection<F> map(Function fun,
                                         java.util.Collection<E> col)
applies the Function fun to each element of the Collection col. The results of the function applications are collected into a new collection, which is returned as result.

Type Parameters:
E - element type of input Collection
F - element type of output Collection
Parameters:
fun - the Function to be applied to all elements of Collection col
col - the collection to be mapped
Returns:
the resulting collection

map

public <E,F> java.util.Collection<F> map(Function fun,
                                         java.util.Iterator<E> iter)
applies the Function fun to each element of the Iterator iter. The results of the function applications are collected into a new collection, which is returned as result.

Type Parameters:
E - the element type of the input Iterator
F - the element type of the output Collection
Parameters:
fun - the Function to be applied to each element of the Iterator
iter - the input Iterator
Returns:
the resulting Collection

exists

public <E> boolean exists(Function predicate,
                          java.util.Collection<E> collection)
if the Function predicate evaluates to true for any element of the input collection true is returned, else false

Type Parameters:
E - the element Type of the input Collection
Parameters:
predicate - the predicate Function
collection - the input Collection
Returns:
true or false

forAll

public <E> boolean forAll(Function predicate,
                          java.util.Collection<E> collection)
if the Function predicate evaluates to true for all elements in the input collection, true is returned, else false.

Type Parameters:
E - the element type of the input collection
Parameters:
predicate - the predicate Function
collection - the input Collection
Returns:
true or false

filter

public <E> java.util.Collection<E> filter(Function predicate,
                                          java.util.Collection<E> collection)
all elements of the input collection that match the predicate are collected into a new Collection. This Collection is returned as result

Type Parameters:
E - the element type of the input collection
Parameters:
predicate - the predicate Function
collection - the input Collection
Returns:
the filtered Collection

foldright

public <E,F> F foldright(Function fun,
                         F acc,
                         java.util.Iterator<E> iter)
performs a foldright of Function fun over Iterator iter. Useful operation to compute a total based on all elements of an Iterator.

Type Parameters:
E - The element type of the Iterator
F - the result type
Parameters:
fun - the accumulating function
acc - the accumulator
iter - the input Iterator
Returns:
the result of type

foldright

public <E,F> F foldright(Function fun,
                         F acc,
                         java.util.Collection<E> col)
performs a foldright of Function fun over a Collection. Useful operation to compute a total based on all elements of a Collection

Type Parameters:
E - the element type of the input collection
F - the result type
Parameters:
fun - the accumulating function
acc - the accumulator that stores the results
col - the input Collection
Returns:
the reuslt of type

foldleft

public <E,F> F foldleft(Function fun,
                        F acc,
                        java.util.Iterator<E> iter)
performs a foldleft operation of Function fun over Iterator iter.

Type Parameters:
E - the element type of the input Iterator
F - the result type
Parameters:
fun - the accumulating Function
acc - the accumulator
iter - the input operator
Returns:
the result of type

foldleft

public <E,F> F foldleft(Function fun,
                        F acc,
                        java.util.Collection<E> col)
performs a foldleft operation of Function fun over the Collection col.

Type Parameters:
E - the element type of the input Collection
F - the result type
Parameters:
fun - the accumulating function
acc - the accumulator
col - the input collection
Returns:
the result of type

compose

public Function compose(Function f,
                        Function g)
composes two functions f(x) and g(x) to f(g(x)).

Parameters:
f - a Funxtion f(x)
g - a Function g(x)
Returns:
the composed Function f o g (x)

compose

public Function compose(Function... functions)
composes a set of functions functions[1..n] to a new function functions[1] o functions[2] o ... functions[n]

Parameters:
functions - the set of functions functions[1..n]
Returns:
the composed function functions[1] o functions[2] o ... functions[n]

constant

public Function constant(java.lang.Object obj)
produces a Function that returns a constant value.

Parameters:
obj - the value to be returned by the constant function.
Returns:
the resulting Function f with f.apply() = obj

$

public <T> T $(java.lang.Class<T> outerInterface,
               java.lang.String methodBody)

toFunction

public Function toFunction(java.lang.reflect.Method method)