Turning repeated code patterns into reusable behaviors
If you can pass around behavior, you can stop rewriting the same program over and over.
def sum_naturals(n):
total, k = 0, 1
while k <= n:
total += k
k += 1
return totaldef sum_cubes(n):
total, k = 0, 1
while k <= n:
total += k ** 3
k += 1
return totaldef pi_sum(n):
total, k = 0.0, 1
while k <= n:
a = 4 * k - 3
total += 1.0 / (a * (a + 2))
k += 1
return 8 * totaldef summation(n, term):
"""Return term(1) + term(2) + ... + term(n)."""
total, k = 0, 1
while k <= n:
total += term(k)
k += 1
return totaldef sum_naturals(n):
return summation(n, lambda k: k)def sum_cubes(n):
return summation(n, lambda k: k ** 3)def sum_squares(n):
return summation(n, lambda k: k ** 2)termdef square_def(x):
return x * x
square_lambda = lambda x: x * x
def apply_twice(f, x):
return f(f(x))
if __name__ == "__main__":
print(square_def(5), square_lambda(5))
print(apply_twice(lambda t: t + 3, 10))
def make_adder(n):
def adder(x):
return x + n
return adder
if __name__ == "__main__":
add_three = make_adder(3)
print(add_three(4))
print(make_adder(10)(-2))
def compose(f, g):
def h(x):
return f(g(x))
return hdef make_adder(n):
return lambda x: x + ndef square(x):
return x * xif __name__ == "__main__":
f = compose(square, make_adder(1))
print(f(5))compose(square, make_adder(1))(5)def improve(update, close, guess, max_steps=100):
steps = 0
while steps < max_steps and not close(guess):
guess = update(guess)
steps += 1
return guessdef newton_update(g, dg):
"""Newton's method: x - g(x)/g'(x)"""
return lambda x: x - g(x) / dg(x)if __name__ == "__main__":
# sqrt(2): solve g(y) = y**2 - 2 = 0
g = lambda y: y**2 - 2
dg = lambda y: 2 * y
close = lambda y: abs(g(y)) < 1e-10update = newton_update(g, dg)update, closedef curry2(f):
"""Transform f(x, y) into a function that can be called as f(x)(y)."""
return lambda x: (lambda y: f(x, y))
if __name__ == "__main__":
curried_pow = curry2(pow)
print(curried_pow(2)(10))
# partial application via currying
pow2 = curried_pow(2)
print(pow2(5), pow2(6))
summation(5, lambda k: 2*k)
Powered by SciMigo AI Tutor - https://scimigo.com