Created
November 22, 2017 16:32
-
-
Save joaoqalves/a85e7e47839de5561fdb525cfeaea656 to your computer and use it in GitHub Desktop.
Currying functions in Python
This file contains hidden or 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
#!/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