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(9119)
# map_f
# map_f
# comp2
# comp2
# comp2
# s
# Traceback:
# in
# in
# in
# in
# 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.