Warm-up exercise: What is mystery?
val lst = List("Mary", "had", "a", "little", "lamb")
val mystery = lst.map(_.length)
return and this{ Type1 arg1, Type2 arg2, ... => stat1, stat2, ..., expr }
{ String a, String b => a.length() - b.length() }
{ String, String => int }invoke to call closure
void sort(String[] a, { String, String => int } comp) {
... if (comp.invoke(a[i], a[j]) < 0) ...
}
button1.addActionListener({ ActionEvent event => textField.setText(...); })
{ ActionEvent => void } closure to
new ActionListener() {
public void actionPerformed(ActionEvent event) { // single method
closure.invoke(event);
}
}
doRead(new FileInputStream("foo.dat"), { InputStream in =>
...
in.read();
...
});
doRead(InputStream in : new FileInputStream("foo.dat")) {
// looks like a regular block
...
in.read();
...
}
methodCall(Type1 param1, ...
Typen paramn : expr1, ...
exprm) block
is rewritten to
methodCall(expr1, ... exprm, {
Type1 param1, ... Typen paramn
=> block })
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));
}
}
[a, had, Mary, lamb, little]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");
}
}
DoneNo asterisk, then Donereturn Mean?returnreturn; returns from
enclosing block==> instead of
=>
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)
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?
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); });
}
}
=> with ==>. What
happens when you run the program? Why?