C311 Spring 1997 -- Programming Languages

Assignment 11: Closures in Java

Due Sunday, April 27, 11:59pm

Translate the following program using higher-order functions into Java using the method presented in class and Script 12.

import java.util.Vector;
import java.util.Enumeration;

class MapVector extends Vector {
  MapVector map((Object) -> Object f) {
    MapVector newMV = new MapVector();
    for (Enumeration e = this.elements() ; e.hasMoreElements() ;) {
      newMV.addElement(f(e.nextElement()));
    }
    return newMV;
  }
}
public class TestMapVector {
  (Object) -> Object curryConcat(String s1) {
    return fun (Object s2) -> Object {
      return (Object)(s1 + (String)s2);
    }
  }  
  public static void main(String[] args) {
    MapVector things = new MapVector();
    TestMapVector tester = new TestMapVector();
    things.addElement("one");
    things.addElement("two");
    things.addElement("three");
    MapVector mv = things.map(tester.curryConcat("plus "));
    System.out.println(mv);
  }
}
      
Here are some instructions of using Java locally, and you may want to use this Makefile to compile your code.

Submission

As before, but refer to "handin 11". However, this assignment won't be automatically graded.


Gustavo Gomez / ggomezes@cs.indiana.edu
Last modified: Sun Apr 20 15:17:14 EST