Useful code which uses reduce()?
functional-programming, python
Solution
The other uses I've found for it besides + and * were with and and or, but now we have `any` and `all` to replace those cases.
`foldl` and `foldr` do come up in Scheme a lot...
Here's some cute usages:
Flatten a list
Goal: turn `[[1, 2, 3], [4, 5], [6, 7, 8]]` into `[1, 2, 3, 4, 5, 6, 7, 8]`.
reduce(list.__add__, [[1, 2, 3], [4, 5], [6, 7, 8]], [])
List of digits to a number
Goal: turn `[1, 2, 3, 4, 5, 6, 7, 8]` into `12345678`.
Ugly, slow way:
int("".join(map(str, [1,2,3,4,5,6,7,8])))
Pretty `reduce` way:
reduce(lambda a,d: 10*a+d, [1,2,3,4,5,6,7,8], 0)
Problem
Does anyone here have any useful code which uses reduce() function in python? Is there any code other than the usual + and * that we see in the examples? Refer Fate of reduce() in Python 3000 by GvR