Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.