|
| ||
|
|
Start of topic | Skip to actions
Lab 08 – Anonymous ClassesAnonymous classes in Java are analogous to lambda-expressions in Scheme. The purpose of this lab is to practice writing such classes. We will use the Intermediate language level for this lab.Lambda-expressions in JavaThe abstract concept of a unary function can be modeled by the following interface.
interface UnaryFun {
Object apply(Object arg);
}
An anonymous UnaryFun class has the following form
new UnaryFun() {
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.
Finger exercises:
IMPORTANT CONCEPT: CLOSURENotice in exercise #5 how the anonymous inner classnew UnaryFun 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.
In functional programming, the closure of a function (lambda) consists of the function itself and the environment in which the function is defined. In Java, a function is replaced by a class. An inner class is only defined in the context of its outer object (and the outer object of the outer object, etc...). An inner class together with its nested sequence of outer objects in which the inner class is well-defined is the equivalent of the notion of closure in functional programming. Such a notion is extremely powerful. Just like knowing how to effectively use lambda expressions and higher order functions is key to writing powerful functional programs in Scheme, effective usage of anonymous inner classes is key to writing powerful OO programs in Java.
More ExercisesHere is the stub code for ObjectList and its concrete subclasses.
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(UnaryFun 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(UnaryFun f) { return null; /*to do */ }
String listString() { return "()"; }
String listStringHelp() { return ""; }
}
class ConsObjectList extends ObjectList {
Object first;
ObjectList rest;
ObjectList map(UnaryFun f) { return null; /* ... rest.map(f) ...to do*/ }
String listString() { return "(" + first + rest.listStringHelp() + ")"; }
String listStringHelp() { return " " + first + rest.listStringHelp(); }
}
Topic Actions: Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r6 < r5 < r4 < r3 < r2 | More topic actions
Webs: Main | TWiki | Africa | CPSX | EmbeddedSystems | Gpce | Houston | International | K12 | MetaOCaml | MulticoreOCR | ProgrammingLanguages | RAP | RIDL | Sandbox | SpeechClub | Teaching | Texbot | WG211 Web Actions: | |
This work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.