...
The abstract concept of a unary function can be modeled by the following interface.
Code Block |
---|
interface UnaryFunLambda { Object apply(Object arg); } |
An anonymous UnaryFun Lambda class has the following form
Code Block |
---|
new UnaryFunLambda() { Object apply(Object arg) { // concrete code to compute something and return the result // …... } } |
Since Object is the superclass of all concrete classes in Java, in practice, we often have to perform type-casting on the arg
in order to perform the desired operation. More than often, we need to type-cast the returned result in order to make proper use of it.
...
Use DrJava to compile the above UnaryFun Lambda interface.
Now create and compile a new file containing the following class.
...
Add the following method to FingerExercisesLab8Exercises.
Code Block |
---|
Object doSomething() { return new UnaryFunLambda() { Object apply(Object arg) { return (Integer)arg * (Integer)arg;} }.apply(5); } |
...
Code Block |
---|
int x_minus_y(int x, int y) { return (Integer)new UnaryFunLambda() { Object apply(Object arg) { return (Integer)arg - y;} }.apply(x); } |
...
What does this anonymous inner class do?
Code Block |
---|
UnaryFunLambda whatIsIt() { return new UnaryFunLambda() { Object apply(Object s1) { return new UnaryFunLambda() { Object apply(Object s2) { return new UnaryFunLambda() { Object apply(Object s3) { return s1 + " " + s2 + " " + s3; } }; } }; } }; } |
...
Code Block |
---|
public class Currying { UnaryFunLambda whatIsIt() { return new UnaryFunLambda() { public Object apply(final Object s1) { return new UnaryFunLambda() { public Object apply(final Object s2) { return new UnaryFunLambda() { public Object apply(final Object s3) { return s1 + " " + s2 + " " + s3; } }; } }; } }; } } |
...
Notice in exercise #5 how the anonymous inner class new !UnaryFunLambda
inside of x_minus_y
is allowed to reference y in its computation? y is said to be in the closure of this anonymous inner class.
...
Code Block |
---|
abstract class ObjectList { ObjectList cons(Object n) { return new ConsObjectList(n, this); } /** Applies f to each element in this list and returns the list containing * the results of this application. */ abstract ObjectList map(UnaryFunLambda f); /** Returns a String representation of this list as specified in each concrete subclass. */ abstract String listString(); /** Returns s String containing the elements of this, where each element is preceded by ' ' */ abstract String listStringHelp(); } class EmptyObjectList extends ObjectList { static EmptyObjectList ONLY = new EmptyObjectList(); private EmptyObjectList() { } ObjectList map(UnaryFunLambda f) { return null; /*to do */ } String listString() { return "()"; } String listStringHelp() { return ""; } } class ConsObjectList extends ObjectList { Object first; ObjectList rest; ObjectList map(UnaryFunLambda f) { return null; /* ... rest.map(f) ...to do*/ } String listString() { return "(" + first + rest.listStringHelp() + ")"; } String listStringHelp() { return " " + first + rest.listStringHelp(); } } |
...