Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 787
benhsu Avatar asked Feb 17 '11 00:02

benhsu


People also ask

Why was reduce removed from Python?

The arguments against reduce are that it tends to be misapplied, harms readability and doesn't fit in with the non-functional orientation of Python.

Does Python have a reduce function?

Python's reduce() is a function that implements a mathematical technique called folding or reduction. reduce() is useful when you need to apply a function to an iterable and reduce it to a single cumulative value.

Is reduce faster than for loop Python?

Indeed, the reduce is much faster than the for when using iadd in the for.

How do you create a reduce function in Python?

Reduce function is an inbuilt function in python whose major task is to give aggregate value as an output. Syntactically it is written as; reduce(func,iter_obj) here reduce operation will be done on “func” function and “iter_obj” is implying the iterable data values used to return result of output.


2 Answers

No. It might eventually, though functional stuff like that isn't a core focus of Guava. See this issue.

like image 64
ColinD Avatar answered Oct 19 '22 12:10

ColinD


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.)

like image 43
Stephen C Avatar answered Oct 19 '22 13:10

Stephen C