Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Functional programming in Java (dzone.com)
6 points by mariofusco on May 24, 2010 | hide | past | favorite | 1 comment


If you'll indulge me in some nostalgia, quite some time ago I wrote a similar Apache Commons (it was Jakarta Commons at the time) library called "functor", essential trying to solve the problem of functions-as-first-class-objects-are-missing-in-Java (but without resorting to byte code manipulation or anything fancy like that; I may not have been clever enough for that). This was just prior to the introduction of generics into the core language, if memory serves.

So for example, Pratik's first example (iterate through a Collection of Strings and return the elements starting with "inv") would be written like:

  List<String> list = Arrays.asList("invalid1", " invalid2");
  return new FilteredIterator(list.iterator(),new UnaryFunctor() { boolean test(String str) { return str.startsWith("inv"); });
although as a practical matter most users would have predefined constant functions, predicates, and helpers, so the second line would read more like:

  return F.filter(list.iterator(),F.startsWith("inv"));
(Yes, Java is verbose.)

It's actually a pretty well tested (100% coverage) and documented library that I used on a number of personal and work projects. but never bothered to push it out of the commons "sandbox". You can find it at http://commons.apache.org/sandbox/functor/. The examples (http://commons.apache.org/sandbox/functor/examples.html) are kind of fun. We were experimenting with a test-first, pseudo-literate programming documentation style.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: