Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

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.

Finger exercises:

HTML
<ol>
HTML
<li>

Use DrJava to compile the above UnaryFun Lambda interface.

HTML
</li>

...


...

<li>

Now create and compile a new file containing the following class.

Code Block
    class Lab8Exercises {
        static Lab8Exercises ONLY = new Lab8Exercises ();
        private Lab8Exercises () {}
    }
HTML
</li>
HTML
<li>

Add the following method to FingerExercisesLab8Exercises.

Code Block
    Object doSomething() {
        return new UnaryFunLambda() {
           Object apply(Object arg) {
                return (Integer)arg * (Integer)arg;}
        }.apply(5);
    }

(Yes, the code is hard to read because of the “ugly” "ugly" syntax.) What does it do? Use the Interactions pane to test it out.

...

...

</li>
HTML
<li>

Now remove the type cast (Integer) on arg in the above code. Does it compile? Do you see why the type cast is necessary? OK, put back the type cast so that everything compile

...


...

</li>
HTML
<li>

Add the following method.

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 it do?
Now remove the first Integer type cast that immediately follows the return in the above code. Does it compile? Put pack the type cast so that everything compiles.

HTML
</li>
HTML
<li>

The method in exercise 5 is an example of what is called “Curry"Curry-ing”ing"( in honor of the mathematician Curry) where a function of two variables x, y is expressed a function of x whose value is a function of y. Any function of two variables, f (x, y), can be represented as a combination of two functions, each of which is a function of one variable. For example,

...

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;
                        }
                    };
                }
            };
        }
    };
}   

Actually the above does not compile in the Intermediate language level. In full Java syntax, the code would like the following (and would compile):

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;
                            }
                        };
                    }
                };
            }
        };
    }   
}
HTML
</li>
html
</ol>

IMPORTANT CONCEPT: CLOSURE

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(); }
}
HTML
<ol start="7">
HTML
<li>

Implement the map method for ObjectList .

HTML
</li>
HTML
<li>

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list L contains integers, returns the list of booleans
     * whose n-th element is true if the number at the n-th element in L is
     * positive, false otherwise.
     */
    ObjectList checkPositive(ObjectList L) {
        return null; // to do
    }
HTML
</li>
HTML
<li>

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list L contains integers, returns the list whose n-th
     * element is the square of the n-th element L.
     */
    ObjectList squareList(ObjectList L) {
        return null; // to do
    }
HTML
</li>
HTML
<li>

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list L contains integers, returns the list of integers
     * whose n-th element is the lesser of the n-th element of L and upperBound
     */
    Object cutOff(ObjectList L, int upperBound) {
        return null; // to do
    }
HTML
</li>
HTML
<li>

Create and compile a file containing the following class

...

Now write the lookup method for ObjectList .

HTML
</li>

...

Add and implement the following method for class Lab8Exercises.

Code Block
    /**
     * Assuming the list dict contains Pairs, returns the list of Objects whose
     * n-th element is the Pair in dict whose key is the n-th element of the
     * list L.
     */
    ObjectList translate(ObjectList L, ObjectList dict) {
        return null; // to do
    }
HTML
</li>
HTML
</ol>

Access Permissions: (Please don't edit)

...