Search code examples
javaguavafunctional-programming

Does guava have an equivalent to Python's reduce function?


Does guava (or another java library) have something like reduce() function in Python?

I'm looking for something like this http://docs.python.org/library/functions.html#reduce


Solution

  • I've not (yet) managed to find any Java collections libraries that support map and reduce. (I exclude map/reduce functionality in parallel / distributed processing frameworks ... because you need a "big" problem for these frameworks to be worthwhile.)

    Probably, the reason for this "lack" is that map/reduce coding without closures is just too cumbersome. Too much boilerplate code, too much heavy-weight syntax. Since the main point of using map / reduce primitives on simple collections is to make your code simple and elegant ...


    @CurtainDog contributed a link to lambdaj. That does the kind of thing that the OP is after (though there's no method specifically called reduce). But it illustrates what I was saying about boilerplate. Notice that many of the higher order operations involve creating classes that extend one or other of the Closure classes.

    (FWIW, I think that the Lambda.aggregate(...) methods are the lambdaj analog of reduce.)