Review

Warm-up exercise: What is mystery?

val lst = List("Mary", "had", "a", "little", "lamb")
val mystery = lst.map(_.length)

http://ActiveLecture.org

Closures in Java

BGGA Syntax

Closure Conversion

Control Invocation Syntax

What Does This Program Do?

import java.util.*;

public class Puzzle1 {
   public static void main(String[] args) {
      String[] foo = new String[] { "Mary", "had", "a", "little", "lamb" };
      Arrays.sort(foo, { String a, String b => return a.length() - b.length(); });
      System.out.println(Arrays.toString(foo));
   }
}
  1. It prints [a, had, Mary, lamb, little]
  2. It compiles but it doesn't print anything
  3. It doesn't compile

http://ActiveLecture.org

And This?

import java.io.*;
public class Puzzle2 {
   public static void doRead(InputStream in, {InputStream ==> void throws IOException } body) throws IOException {
      try { body.invoke(in); } finally { in.close(); }
   }
   public static void main(String[] args) throws IOException {
      doRead(InputStream in : new FileInputStream("Puzzle2.java")) {
         int c = 0;
         while ((c = in.read()) != -1) {
            if (c == '*') return;
         }
         System.out.println("No asterisk");
      }
      System.out.println("Done");
   }
}
  1. It prints Done
  2. It prints No asterisk, then Done
  3. It prints nothing

http://ActiveLecture.org

What Does return Mean?

Lab

Part 1

  1. Download the BGGA compiler from http://www.javac.info/

    Rewrite this Java program to use BGGA

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Lab7 {
        public static void main(String[] args) {
            JFrame frame = new JFrame();
      
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JTextArea textArea = new JTextArea(20, 50);
            JButton button1 = new JButton("Click me!");
            JButton button2 = new JButton("Exit");
            
            button1.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent event) { 
                     textArea.append("Hello!\n"); 
                  }});  
            button2.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent event) { 
                     System.exit(0); 
                  }});
            
            JPanel panel = new JPanel();
            panel.add(button1);
            panel.add(button2); 
            frame.add(panel, BorderLayout.NORTH);
            frame.add(textArea);
            frame.pack();
            frame.setVisible(true);
        }
    }

    Replace the action listener instances with BGGA closure instances. Remove the final. Compile and run. What is your code?

    NOTE: To compile and run, use /path/to/closures-date/bin/javac and /path/to/closures-date/bin/java

    (If you use Windows and not Cygwin, add .bat to each command)

Part 2

  1. The Properties class is often used to store name/value associations. It is a bit of a hassle to iterate over properties:
    Enumeration<?> names = prop.propertyNames(); 
    while (names.hasMoreElements()) {
       String name = names.nextElement().toString();
       String value = prop.getProperty(name);
       ... do something with name and value ...
    }

    In BGGA, implement a method so that one can call

    forEachProperty(String name, String value : prop) {
       ... do something with name and value ...
    }

    Then try

    forEachProperty(String name, String value : System.getProperties()) {
       System.out.println(name + "->" + value)
    }

    What is the code for your forEachProperty method?

Part 3

  1. Try compiling the Puzzle3 class with BGGA. What error message do you get?
    import java.util.*;
    
    class Utils {
       public static <T> void forEach(Iterable<T> seq, {T => void } fct) {
          for (T elm : seq)
             fct.invoke(elm);
       }
    }
    
    class Puzzle3 {
       public static void main(String[] args) {
          List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
          Utils.forEach(nums, { Integer arg => 
                   if (arg == 3) return; 
                   System.out.println(arg); });
       }
    }
  2. Now replace both => with ==>. What happens when you run the program? Why?