Chapter 6
0.1731
latitude(start(point))
35.505665
Our initial point object is a nested three tuple with (0) - a starting position, (1) - the
ending position, and (2) - the distance. We extracted various fields using our helper
functions.
Given these helpers, we can locate the northern-most starting position for the legs in
each bin:
for distance in sorted(by_distance):
print(distance, max(by_distance[distance],
key=lambda pt: latitude(start(pt))))
The data that we grouped by distance included each leg of the given distance.
We supplied all of the legs in each bin to the max() function. The key function
we provided to the max() function extracted just the latitude of the starting
point of the leg.
This gives us a short list of the northern-most legs of each distance as follows:
0.0 ((35.505665, -76.653664), (35.508335, -76.654999), 0.1731)
5.0 ((38.845501, -76.537331), (38.992832, -76.451332), 9.7151)
10.0 ((36.444168, -76.3265), (36.297501, -76.217834), 10.2537)
125.0 ((27.154167, -80.195663), (29.195168, -81.002998), 129.7748)
Writing higher-order reductions
We'll look at an example of a higher-order reduction algorithm here. This will
introduce a rather complex topic. The simplest kind of reduction develops a single
value from a collection of values. Python has a number of built-in reductions,
including any(), all(), max(), min(), sum(), and len().
As we noted in Chapter 4, Working with Collections, we can do a great deal of statistical
calculation if we start with a few simple reductions such as the following:
def s0(data):
return sum(1 for x in data) # or len(data)
def s1(data):
return sum(x for x in data) # or sum(data)
def s2(data):
return sum(x*x for x in data)