Skip to content

Instantly share code, notes, and snippets.

@dhilst
Last active August 14, 2017 19:22
Show Gist options
  • Select an option

  • Save dhilst/1f45b4a8273142b8a262805dcfe4b46b to your computer and use it in GitHub Desktop.

Select an option

Save dhilst/1f45b4a8273142b8a262805dcfe4b46b to your computer and use it in GitHub Desktop.
from functools import partial, reduce
from inspect import signature, Parameter
import operator as o
def compose(*funcs):
def compose2(f, g):
return lambda *args, **kwargs: f(g(*args, **kwargs))
return reduce(compose2, funcs)
def curry(f):
def _(arg):
try:
return f(arg)
except TypeError:
return curry(partial(f, arg))
return _
inc = partial(o.add, 1)
double = partial(o.mul, 2)
inc_double = compose(double, inc)
print(inc_double(2))
@curry
def add(a,b):
return a + b
@curry
def trimul(a,b,c):
return a * b * c
print(add(1)(2))
print(trimul(2)(2)(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment