2018年5月3日 星期四

[ Python 文章收集 ] Currying in Python

Source From Here 
General Ideas 
In mathematics and computer science, Currying is the technique of breaking down the evaluation of a function that takes multiple arguments into evaluating a sequence of single-argument functions.f Currying is also used in theoretical computer science, because it is often easier to transform multiple argument models into single argument models. 

Composition of Functions 
We define the composition h of two function f and g: 
h(x) = g(f(x))

In the following Python example, the composition of two functions is a chaining process in which the output of the inner function becomes the input of the outer function: 
  1. def compose(g, f):  
  2.     def h(x):  
  3.         return g(f(x))  
  4.     return h  
We will use our compose function in the next example. Let's assume, we have a thermometer, which is not working accurate. The correct temperature can be calculated by applying the function readjust to the temperature values. Let us further assume that we have to convert our temperature values into degrees fahrenheit. We can do this by applying compose to both functions: 
  1. def celsius2fahrenheit(t):  
  2.     return 1.8 * t + 32  
  3. def readjust(t):  
  4.     return 0.9 * t - 0.5  
  5. convert = compose(readjust, celsius2fahrenheit)  
  6. convert(10), celsius2fahrenheit(10)  
The above Python code returned the following output: 
(44.5, 50.0)

The composition of two functions is generally not commutative, i.e. compose(celsius2fahrenheit, readjust) is different from compose(readjust, celsius2fahrenheit) 
  1. convert2 = compose(celsius2fahrenheit, readjust)  
  2. convert2(10), celsius2fahrenheit(10)  
The above Python code returned the following output: 
(47.3, 50.0)

"compose" with Arbitrary Arguments 
The function compose which we have just defined can only copy with single-argument functions. We can generalize our function compose so that it can cope with all possible functions: 
  1. def compose(g, f):  
  2.     def h(*args, **kwargs):  
  3.         return g(f(*args, **kwargs))  
  4.     return h  
Example using a function with two parmameters: 
  1. def BMI(weight, height):  
  2.     return weight / height**2  
  3. def evaluate_BMI(bmi):  
  4.     if bmi < 15:  
  5.         return "Very severely underweight"  
  6.     elif bmi < 16:  
  7.         return "Severely underweight"  
  8.     elif bmi < 18.5:  
  9.         return "Underweight"  
  10.     elif bmi < 25:  
  11.         return "Normal (healthy weight)"  
  12.     elif bmi < 30:  
  13.         return "Overweight"  
  14.     elif bmi < 35:  
  15.         return "Obese Class I (Moderately obese)"  
  16.     elif bmi < 40:  
  17.         return "Obese Class II (Severely obese)"  
  18.     else:  
  19.         return "Obese Class III (Very severely obese)"  
  20. f = compose(evaluate_BMI, BMI)  
  21. weight = 1  
  22. while weight > 0:  
  23.     weight = float(input("weight (kg) "))  
  24.     height = float(input("height (m) "))  
  25.     print(f(weight, height))  
Output: 
weight (kg) 73 
height (m) 1.76 
Normal (healthy weight) 
weight (kg) 75 
height (m) 1.76 
Normal (healthy weight)


沒有留言:

張貼留言

[ FP In Python ] Ch1. (Avoiding) Flow Control

Preface   In typical imperative Python programs—including those that make use of classes and methods to hold their imperative code—a block...