Skip to content

Instantly share code, notes, and snippets.

@renato04
Created May 17, 2019 13:21
Show Gist options
  • Save renato04/2c47cf54480d4d9d71cfd5a8b935128e to your computer and use it in GitHub Desktop.
Save renato04/2c47cf54480d4d9d71cfd5a8b935128e to your computer and use it in GitHub Desktop.
Python High order function samples
# Composistion
def f(x):
return x + 2
def g(h, x):
return h(x) * 2
print(g(f, 42))
# Closure
def addx(x):
def _(y):
return x + y
return _
add2 = addx(2)
add3 = addx(3)
print(add2(2), add3(3))
# Currying
def a(x, y):
return x*y
def f2(x):
def _(y):
return (a(x, y))
return _
print(f2(2))
print(f2(2)(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment