Review

Warm-up exercise: What is mystery?

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

http://ActiveLecture.org

Closures in Java

BGGA Syntax

Closure Conversion

Control Invocation Syntax

What Does This Program Do?

import java.util.*;

public class Puzzler {
   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

And This?

import java.io.*;
public class Puzzle {
   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("Puzzle.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

What Does return Mean?

CICE

ARM

Lab