September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
54
Task/Currying/Python/currying-5.py
Normal file
54
Task/Currying/Python/currying-5.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# AUTOMATIC CURRYING AND UNCURRYING OF EXISTING FUNCTIONS
|
||||
|
||||
|
||||
# curry :: ((a, b) -> c) -> a -> b -> c
|
||||
def curry(f):
|
||||
return lambda a: lambda b: f(a, b)
|
||||
|
||||
|
||||
# uncurry :: (a -> b -> c) -> ((a, b) -> c)
|
||||
def uncurry(f):
|
||||
return lambda x, y: f(x)(y)
|
||||
|
||||
|
||||
# EXAMPLES --------------------------------------
|
||||
|
||||
# A plain uncurried function with 2 arguments,
|
||||
|
||||
# justifyLeft :: Int -> String -> String
|
||||
def justifyLeft(n, s):
|
||||
return (s + (n * ' '))[:n]
|
||||
|
||||
|
||||
# and a similar, but manually curried, function.
|
||||
|
||||
# justifyRight :: Int -> String -> String
|
||||
def justifyRight(n):
|
||||
return lambda s: (
|
||||
((n * ' ') + s)[-n:]
|
||||
)
|
||||
|
||||
|
||||
# CURRYING and UNCURRYING at run-time:
|
||||
|
||||
def main():
|
||||
for s in [
|
||||
'Manually curried using a lambda:',
|
||||
'\n'.join(map(
|
||||
justifyRight(5),
|
||||
['1', '9', '10', '99', '100', '1000']
|
||||
)),
|
||||
|
||||
'\nAutomatically uncurried:',
|
||||
uncurry(justifyRight)(5, '10000'),
|
||||
|
||||
'\nAutomatically curried',
|
||||
'\n'.join(map(
|
||||
curry(justifyLeft)(10),
|
||||
['1', '9', '10', '99', '100', '1000']
|
||||
))
|
||||
]:
|
||||
print (s)
|
||||
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue