I ended my previous discussion of functors, you might say, perfunctorily. Before diving on the next part of this essay let me add the following example of the fore mentioned functor in action.
The idea of the following example is to mimic a scripting idiom for processing text files.
LineProcessor lp = new LineProcessor(new File("foo.foo"));
lp.lookEachLine(new IStringFunctor() {
public Object call(String s) {
System.out.println(s);
}
});
Compare this to some Ruby code that does the same thing:
aFile = File.new(“foo.foo”)
aFile.each_line( ) do |line|
puts line.dump
end
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("foo.foo"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// do something with the exception?
}
finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// do something with the exception?
}
}
}