Skip to content

Instantly share code, notes, and snippets.

@joaoqalves
Created November 22, 2017 16:32
Show Gist options
  • Save joaoqalves/a85e7e47839de5561fdb525cfeaea656 to your computer and use it in GitHub Desktop.
Save joaoqalves/a85e7e47839de5561fdb525cfeaea656 to your computer and use it in GitHub Desktop.
Currying functions in Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
def curried(n):
def curry(fn):
def _inner(*args):
if len(args) < n:
return curried(n - len(args))(partial(fn, *args))
return fn(*args)
return _inner
return curry
@curried(2)
def is_multiple(div, num):
return num % div == 0
mul_3 = is_multiple(3)
mul_3 = is_multiple(5)
print(mul_3)
print(mul_3(3))
print(mul_5(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment