This little function is really, really cool because it allows you to build up more interesting functions by piecing together a bunch of small, useful ones.
def upper(s):
return s.upper()
def exclaim(s):
return s + '!'
# instead of this
really_angry = lambda s: exclaim(exclaim(upper(s)))
really_angry('napster bad') # NAPSTER BAD!!
# we can do this
really_angry = compose(upper, exclaim, exclaim)
really_angry('fire good') # FIRE GOOD!!
# and
import operator as op
from functools import partial as p
max(map(compose(p(op.add, 1), p(op.mul, 3)), (1, 2, 3, 4)))
`compose` is a neat function and worth exploring. This is a cool book and I always hope Python gets more light shone on its FP-friendly features.
For compose to really shine, you need to be able to curry/partially apply functions. This part of things is made much more difficult than necessary because of Python's unnecessarily neutered lambda syntax (in fact, I don't think one can claim that Python is FP friendly until this decision is corrected).
It's also worth noting that reduce() was removed as a builtin for Python 3.
I started with something like that when I was missing function composition in Python. Eventually I ended up with a library [1] including a bunch of other stuff for getting rid of some of the duct tape code you usually need when you just want to compose some functions.
You can write some pretty neat looking concise code with this, but also may regret it later when it comes to debugging, especially when lazy evaluation is involved (which is usually the case). The stacktraces tend to be not so helpful...
`compose` can be simpler:
This little function is really, really cool because it allows you to build up more interesting functions by piecing together a bunch of small, useful ones. `compose` is a neat function and worth exploring. This is a cool book and I always hope Python gets more light shone on its FP-friendly features.