I know that what I am going to offer is not perfect, but I think it is as close as possible to my contribution. This is an interesting problem to solve, even if it is not the most traditional shortening application.
The key problem, apparently, is tracking the distance from point to point without overwriting the points themselves - adding another “dimension” to each point gives you a field with which you can track the mileage.
iterable = ((1,2,0), (3,4,0), (1,8,0)) # originally ((1,2), (3,4), (1,8)) from math import sqrt def func(tup1, tup2): '''function to pass to reduce''' # extract coordinates x0 = tup1[0] x1 = tup2[0] y0 = tup1[1] y1 = tup2[1] dist = tup1[2] # retrieve running total for distance dx = x1 - x0 # find change in x dy = y1 - y0 # find change in y # add new distance to running total dist += sqrt(dx**2 + dy**2) # return 2nd point with the updated distance return tup2[:-1] + (dist,) # eg (3, 4, 2.828)
Now reduce:
reduce(func, iterable)[-1] # returns 7.3005630797457695
Thus, the intermediate tuple of tuples (that is, after one “reduction”) becomes:
((3, 4, 2.8284271247461903), (1,8,0))