Use currying and composition to calculate distance conversions. The first thing you will need in this section
Fantastic news! We've Found the answer you've been seeking!
Question:
Use currying and composition to calculate distance conversions.
- The first thing you will need in this section is the compose() function which accepts a series of functions and reduces it to a single function call. You can write the compose function any way you like, but here are two common implementations:
def compose(*functions):
return reduce(lambda f, g: lambda x: g(f(x)), functions)
OR
compose = lambda *F: reduce(lambda f, g: lambda x: g(f(x)), F)
- Next, implement a series of functions to convert from one unit of measure to another. The idea is that we have the ability to convert from any one unit to another by tying a combination of these methods together in the right order.
- Use the following chart to implement the methods.
- The conversion factor is the value you must multiply by to convert from the input value to the output result.
- Each method accepts a single value and returns the resulting calculation.
- Once all of the functions are implemented, you can start tying them together using compose(). To convert from miles to feet, you would use compose in the following manner:
milesToFeet = compose(miles to yards, yards to feet)
- Now you can execute the milesToFeet() function variable passing in the number of miles you want to convert to feet.
- To go from miles to light-years will require many functions to be chained together.
Here are the conversions you must perform in this exercise and the approximate results (your results may vary, but not by much):
- Convert 2 miles to inches (result: 126720)
- Convert 5 feet to meters (result: 1.524)
- Convert 1 meter to inches (result: 39.37007874015748)
- Convert 10 miles to kilometers (result: 16.09344)
- Convert 1 kilometer to miles (result: 0.6213711922373341)
- Convert 12.7 kilometers to inches (result: 500000.0)
- Convert 500000 inches to kilometers (result: 12.7)
- Convert 9,460,730,472,580,800 meters to light-years (result: 1)
Related Book For
Organizational Behavior Concepts, Controversies, Applications
ISBN: 9780136199601
8th Canadian Edition
Authors: Nancy Langton, Stephen P. Robbins, Timothy A. Judge
Posted Date: