by ptr · 12y ago · 0 comments
let square = map (^ 2)
Real partial application and currying is something I'm longing for in other languages; it's easy to fall for the beauty of the Point Free Style. from functools import partial
square = partial(map, lambda i: i**2)
EDIT: I guess it's still not the same, since the lambda isn't a partial application. But I don't think we can just set the exponent in pow(), we need to create a function that takes the arguments in the reverse order. Maybe: from functools import wraps, partial
def flip(func):
'Create a new function from the original with the arguments reversed'
@wraps(func)
def newfunc(*args):
return func(*args[::-1])
return newfunc
_pow = flip(pow) #Take Y, then X
# Then the equivalent would be
square = partial(map, partial(_pow, 2)
Of course, flip is rather long-winded because it's a generic function, one could just do: _pow = lambda y, x: pow(x, y)