Created
October 5, 2011 10:20
-
-
Save TinnedTuna/1264112 to your computer and use it in GitHub Desktop.
Compositional Style 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
def compose(f, g): | |
return lambda(x):f(g(x)) | |
def composecurry(f): | |
return lambda(x) : compose(f,x) | |
def composevar(*fns): | |
acc = (lambda (x) : x) | |
for f in fns: | |
acc = compose(acc,f) | |
return acc | |
print (compose(lambda(x):(x + 3), (lambda(x):(x*2)))(4)) | |
print (composecurry(lambda (x) : (x+3))(lambda(x):(x*2))(4)) | |
def inc(x): | |
return x+1 | |
def mult3(x): | |
return x*3 | |
print composevar(inc, inc, inc, inc, mult3)(7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment