Skip to content

Instantly share code, notes, and snippets.

@agos
Last active August 29, 2015 14:08
Show Gist options
  • Save agos/4bee946e973fa726852e to your computer and use it in GitHub Desktop.
Save agos/4bee946e973fa726852e to your computer and use it in GitHub Desktop.
identity = lambda x: x
compose = lambda f, g: lambda x: g(f(x))
# Test identity function
print identity(3) is 3
# Test composition
increment = lambda x: x + 1
double = lambda x: x * 2
print compose(increment, double)(3)
print compose(double, increment)(3)
# Test composition respects identity
five = lambda x: 5
print five("WAT") is compose(identity, five)("WAT")
print five("WAT") is compose(five, identity)("WAT")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment