Created
April 26, 2011 17:17
-
-
Save jamiltron/942682 to your computer and use it in GitHub Desktop.
Very basic curry example in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is meant to show trivial examples of currying. | |
# I know there are way more effective methods of doing this | |
# but this is meant to be illustrative, I also want to add | |
# a few non-trivial examples to show how its useful. | |
import operator | |
def curry(func, var): | |
y = var | |
def f(x): | |
return func(x, y) | |
return f | |
if __name__ == '__main__': | |
double = curry(operator.mul, 2) | |
add7 = curry(operator.add, 7) | |
a = double(6) | |
b = add7(2) | |
print("Double 6: %i" %a) | |
print("Add 7 to 2: %i" %b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def volume(width,length,height):
return widthlengthheight
surface_10 = partial(volume,10,10)
volume_for_surface10 = surface_10(100)
print(volume_for_surface10)
#语法糖
@Curry
def volume2(width,length,height):
return widthlengthheight
surface_20 = volume2(2,10)
volume_for_surface20 = surface_20(100)
print(volume_for_surface20)