Created
November 21, 2014 09:05
-
-
Save marchdown/59e34a48ee0c8016fff0 to your computer and use it in GitHub Desktop.
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
from functools import partial | |
def reduce(reducing_function, fs, initial_value=None): | |
fs = iter(fs) | |
v = fs.next() if initial_value is None else initial_value | |
for f in fs: | |
v = reducing_function(v, f) | |
return v | |
square = lambda x: x**2 | |
def s(n): | |
res = str(n+0) | |
print "s" | |
return res | |
def map_f(f): | |
res = partial(map, f) | |
print "map_f" | |
return res | |
def compose(*fs): | |
def comp2(f,g): | |
res = lambda x: f(g(x)) | |
print "comp2" | |
return res | |
return reduce(comp2, fs) | |
# int -> int | |
# by the way of | |
# int -> [char] -> [int] -> [char] -> int | |
# int, str, partial(map(lambda x:int(x)**2)), str | |
squareDigits = compose(int, map_f(str), map_f(square), s) | |
################ trace: | |
# squareDigits(2332) | |
# s | |
# Traceback (most recent call last): | |
# File "<stdin>", line 1, in <module> | |
# File "/code/kodeboek/functional_kata.py", line 23, in <lambda> | |
# res = lambda x: f(g(x)) | |
# File "/code/kodeboek/functional_kata.py", line 23, in <lambda> | |
# res = lambda x: f(g(x)) | |
# File "/code/kodeboek/functional_kata.py", line 9, in <lambda> | |
# square = lambda x: x**2 | |
# TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' | |
# >>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment