Best Java Functor lib: JGA, commons functor, mango

2019-03-16 14:05发布

问题:

I am interested in using functors (function objects) in Java. With quick googling I found these 3 packages:

  • Java Generics Algorithms: http://jga.sourceforge.net/
  • Commons functor: http://commons.apache.org/sandbox/functor/
  • Mango: http://www.jezuk.co.uk/cgi-bin/view/mango

and of 3, JGA seemed like it might have the best design. But I suspect others here who have actually used one or more of the packages might be able to offer more insight as to pros and cons of these (and perhaps other) functor packages.

回答1:

The Google Collections Library provides a Function interface that is similar (though probably not as in depth).



回答2:

Mango

Provides interfaces for 0, 1, and 2 argument functions. Uses Java generics. Good range of algorithms for working with functions.

JGA

Provides classes for 0, 1, 2 and arbitrary number of argument functions. Domain objects subclass these. Uses Java generics. Extensive range of algorithms. Includes JFXG (Java Functor eXpression Grammar) - a parsed language intended to make it easy to create arbitrarily complex functors. Provides multiple algorithms for working with functions.

Apache Commons Functor

Provides interfaces for 0, 1, and 2 argument functions as well as 0, 1 and 2 argument procedures (which return no value). Uses Java generics. Good range of algorithms.

Google Guava (was Google collections)

Provides Function and Predicate interfaces for single argument functions. Uses Java generics. Provides only the compose method for combining functions. Pretty basic.

FunctionalJ

Provides interfaces and classes for 0, 1, 2 and arbitrary number of argument functions. Uses Java generics. Existing methods can be turned into functions via the provided function reflection classes.



回答3:

I hope I won't offend anybody by telling that a function object is not a functor. Look up functor on wikipedia. And please stop misusing this term.



回答4:

swensen.functional, http://www.codeproject.com/KB/java/FunctionalJava.aspx, by Yours Truly.

Provides single method generic interfaces encapsulating 0 through 5 argument functions (FuncX) and procedures (ActionX). Also includes a Predicate functor (abstract class implementing Func2<T,Boolean>) and three others designed for compatibility with legacy Comparator, Runnable, and Callable functors. And to make it all useful, an immutable Iterable type (constructable from all arrays and Iterables) featuring method chaining, lazy evaluation, and functional projections like filter, map, and fold.

Pro: Simple yet effective, no convoluted type hierarchies undermining the spirit of fp.
Con: New and not yet battle tested.



标签: java functor