A higher-order function takes functions as input and returns new functions.
def compliment(f):
"""
Returns the compliment (which is a function) of function f.
"""
def res(*args):
return not f(*args)
return res
def even(n):
return bool(n % 2 == 0)
odd = compliment(even)
Note that the argument of compliment
is not limited to a function
which only takes one argument.
Without the higher-order function compliment
,
we would have to write:
def odd(n):
return not even(n)