Showing posts with label closures. Show all posts
Showing posts with label closures. Show all posts

Monday, August 20, 2007

Contest: Opine On Closures In One Sentence

The first Sticker Contest on POJOs was fun: with the success of the last post, it's time for Sticker Contest #2!

The gist: the ladies stated their case in the nightclubs. Now it's your turn.

Write a single sentence that states the case for (or even against) closures in Java 7. Send your entry to "codetojoy" @t (gmail).

Be earnest. Or creative. Or funny.... The choice is yours, but be brief.

The contest will end August 31. Winners (see rules) will get a free sticker (either "Code to Joy" or "+1 For Closures") and CodeToJoy pays postage.

ps. Here are some more contest rules. (Though note that most likely more than one person from a given country can win.)

pps. If you have written a closure proposal or contributed to the debate with a major article (e.g. Neal Gafter, James Gosling, Brian Goetz, and others), then you're a Diamond Member! Membership has privileges: a free sticker. Just let me know if you're interested.

Sunday, July 8, 2007

Closures in Action: searching jars

A recent blog post talks about the problem of searching jars for class names. I have done something similar in Java, and ported the solution to Groovy.

The code is listed below. Some tidbits:

  • My uncommented Java program was 87 lines. The commented Groovy program is under 50.
  • The program is not fast but is straight-forward. It recursively searches a directory for jars, and searches each jar for a given string (e.g. "org/apache/log4j/Logger").
  • It uses 2 closures. This is a modest example of the power of generic algorithms (e.g. eachFile()) and closures.
  • If you aren't familiar with closures, Groovy is a fantastic way to "test drive" them before deciding on your stance about including them in Java.
// Usage:
// groovy Which [searchDir] [target]
//
// e.g. groovy Which c:\tomcat org/apache/log4j/Logger

import java.io.File;
import java.util.jar.JarFile;

// Closure: if jarEntry's name matches target, print fileName
// NOTE: fileName value comes from enclosing scope
myEntryChecker = {
jarEntry ->
int index = jarEntry.getName().indexOf(target);

if( index != -1 ) {
println "found match in " + fileName;
}
}

// Closure: if file is a jar, apply myEntryChecker
myFileChecker = {
file ->
if( file.isFile() && file.canRead() ) {
fileName = file.getName();

if( fileName.indexOf(".jar") != -1 ) {
JarFile jarFile = new JarFile(file);
jarFile.entries().each(myEntryChecker);
}
}
}

////////////////////////////////////////////////
// static void main(String args[])
//
// todo: sanity check arguments
def fileName
searchDir = args[0]
target = args[1]
println "\nsearching: " + searchDir
println "target: " + target + "\n"

new File(searchDir).eachFileRecurse(myFileChecker)

println "\ndone. "

Friday, June 15, 2007

JSR 3000: Annotation Closures

(Inspired by a post by Weiqi Gao and Wired magazine's Artifacts from the Future).

CtJ Newswire
Parody City, CA
June 2010

Several industry leaders announced today that they will support JSR 3000, an initiative to add closures to the JML (Java Metadata Language). The effort ends much debate among heavyweights in the Java community,which began with the hotly-contested JSR 1969 and bitterly-disputed JSR 2001, which added control structures and objects, respectively, to the JML.

Said one industry leader, "Not only has the JML become a full Turing machine with OO capabilities, but now with annotated closures, we should see entire web frameworks in a single class file. The amount of true Java code will be miniscule, and javac and apt will do most of the work. Once we solve the problem of distributing javac and apt to customers, we will have truly arrived in a new age".

The press announcement offered the following as an example of the new syntax. The JCP website was briefly shutdown due to overwhelming volume.


// print lines of file
@MyFileTraverser extends Object
@Field("file", private)
@Field("myClosure", private)
@Method("traverse", public)
@Invoke("myClosure", "file")
@
@If.exists(args[0])
@New("myTraverser", MyFileTraverser)
@Set("file", args[0])
@Set("myClosure",
@{ String line => System.out.println(line)} )
@Invoke("myTraverser", "traverse")
@Else
@Exit("usage: please pass a file name to apt")
public class HelloWorld {
static public void main(String args) {
System.err.println(
"error: raw JVM bytecode reached.");
System.err.println(
"error: please contact system admin");
}
}