Skip to content

Instantly share code, notes, and snippets.

@conf8o
Last active May 24, 2021 07:18
Show Gist options
  • Save conf8o/50a9b1837ac8fd2a773606b140553f7e to your computer and use it in GitHub Desktop.
Save conf8o/50a9b1837ac8fd2a773606b140553f7e to your computer and use it in GitHub Desktop.
カリー化演算子(lambdaの仕様理解の旅)
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")
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)
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")
f = lambda x: x + i
i = 1
print(f(10))
i = 9990
print(f(10))
11
10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment