Last active
May 24, 2021 07:18
-
-
Save conf8o/50a9b1837ac8fd2a773606b140553f7e to your computer and use it in GitHub Desktop.
カリー化演算子(lambdaの仕様理解の旅)
This file contains 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
import operator as ope | |
from functools import partial | |
lt, le, eq, ne, ge, gt = [(lambda x: partial(o, x)) for o in [ope.lt, ope.le, ope.eq, ope.ne, ope.ge, ope.gt]] | |
test = [f for f in [lt(1), le(1), eq(1), ne(1), ge(1), gt(1)]] | |
print(*test, sep="\n") |
This file contains 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
functools.partial(<built-in function gt>, 1) | |
functools.partial(<built-in function gt>, 1) | |
functools.partial(<built-in function gt>, 1) | |
functools.partial(<built-in function gt>, 1) | |
functools.partial(<built-in function gt>, 1) | |
functools.partial(<built-in function gt>, 1) |
This file contains 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
import operator as ope | |
from functools import partial | |
def currying(o): | |
def f(x): | |
return partial(o, x) | |
return f | |
lt, le, eq, ne, gt, ge = [currying(o) for o in [ope.lt, ope.le, ope.eq, ope.ne, ope.gt, ope.ge]] | |
test = [f for f in [lt(1), le(1), eq(1), ne(1), ge(1), gt(1)]] | |
print(*test, sep="\n") |
This file contains 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
f = lambda x: x + i | |
i = 1 | |
print(f(10)) | |
i = 9990 | |
print(f(10)) |
This file contains 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
11 | |
10000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment