Remember to check the "with heading" box in the lower right corner. Surround code listings with <pre> and </pre>, and if you use < and > symbols you must say & lt ; and & gt ;
2004/10/24 20:57 EST (via web):
Bruce,
Congratulations on an excellent article. I particularly like the term function object that you use, I find it more descriptive than closure, first-class function, functor, etc., hopefully it will catch on!
As an aside from my main post: this blog is almost like a religuous conversion, suddenly you are writting using patterns like adaptor and strategy :)
Back to the main thrust of the post. Whereas Java usually requires you to explicitly state types there is one area where you don't have to, static methods. Therefore if you write a static method:
static < T extends Comparable< T > > UnaryPredicate< T > greaterThan( final T bound ) {
return new GreaterThan< T >( bound );
}
and use a static import then you don't need to say new and you don't need to specify the generic type, it is infered from the argument, i.e. you can say:
greaterThan( 4 )instead of:
new GreaterThan< Integer >( 4 )I find this useful in generic programming to reduce the typing. However, there is a danger that the code becomes write only since the explicit type information generally helps when reading a program.
Congratulations again, the code you have written is truely excelent and demonstrates well the power of generics and also a programming style suitable for Java generics.
Howard.
[[ Thanks! Two comments: no overnight religious conversion here; see "Thinking in Patterns" under "books" on this web site. 2) Generic methods in general perform type capture so the above applies to both static and non static, but as you note the use of static means you don't need the extra "new". -- Bruce ]]
2004/10/26 15:30 EST (via web):
Function objects: http://en.wikipedia.org/wiki/Function_object
2004/10/28 21:24 EST (via web):
Bruce:
I'll comment here, but it pertains to both this and the previous article. As you'd expect, there are a number of efforts in developing libraries along the lines of what you're describing. With my particular project, JGA, the code to implement the sum of a heterogenous collection of numbers would be
List<Integer> integers = Arrays.asList(new Integer(43),
new Integer(65),
new Integer(-1));
Accumulate<Integer> sumInts = new Accumulate<Integer>(new Plus<Integer>(Integer.class));
System.out.println("Integers total " +sumInts.fn(integers.iterator()));
in the case of JGA, it also works with other Number implementations
List<BigDecimal> decimals = Arrays.asList(new BigDecimal("43"),
new BigDecimal("27"),
new BigDecimal("-2"),
new BigDecimal("15"));
Accumulate<BigDecimal> sumDecs =
new Accumulate<BigDecimal>(new Plus<BigDecimal>(BigDecimal.class));
System.out.println("Decimals total " +sumDecs.fn(decimals.iterator()));
I'm sure that you can do something reasonably similar in any of the other
functor-based libraries that are around (Mango, jakarta-commons, etc).
If you pull down the HEAD revision of JGA (or wait until the next release), you'll be able to do it this way:
FunctorParser parser = new FunctorParser(System.in);
BinaryFunctor addInts = parser.parseBinary("x + y", Integer.class, Integer.class);
Accumulate<Integer> accInts = new Accumulate<Integer>(addInts);
System.out.println("Integers total " +accInts.fn(integers.iterator())); BinaryFunctor addDecs = parser.parseBinary("x + y", BigDecimal.class, BigDecimal.class);
Accumulate<BigDecimal> accDecs = new Accumulate<BigDecimal>(addDecs);
System.out.println("Decimals total " +accDecs.fn(decimals.iterator()));
David Hall
http://jga.sf.net/
This example kind of makes my love of Java want to lay down and shoot itself.
>>> def compute(low_boundary, values): ... return reduce(lambda x,y: x*y, filter(lambda low_bound:low_bound>low_boundary, values)) ... >>> compute(4, [1, 2, 3, 4, 5, 6, 7]?) 210
The Python implementation shown above seems more terse, somehow.
Go to http://www.python.org/ maybe if you want more information or a free download of the Python language. If you use MS-Windows, after you get Python installed I would score myself a copy of the free PythonWin? IDE if I were you.
Python 2.4 just came out last week and it adds some really incredible features for solving other "complex" Java and C/C++ problems in a lot less code and time.
The code I wrote above would have worked in Python 1.5 (back in 1997, wasn't it?) and maybe even further back. That is as far as I go back with it and as old as my oldest books on it, so I really cannot say.
But suffice it to say that the Java version of this program will not even come close to being able to compile with any version of Java older than 1.5, which just came out a month or two ago.
Johnny Software http://www.jroller.com/page/johnnysoftware
subtopics:
|