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.
from pipetools import pipe, X, foreach
really_angry = pipe | upper | exclaim | exclaim
or... really_angry = X.upper() | "{0}!" | "{0}!"
(1, 2, 3, 4) > foreach((X + 1) | (X * 3)) | max
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...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'd love let-like syntax in lambdas, something along the lines of
lambda x: f(y) + g(y) for y = expensive_computation(x)
In Python 3, `reduce` can be trivially imported from `functools`.
Re:Functional Programming in Python [pdf]
by gamegoblin on Saturday July 25, 2015 @01:31AM
(#9946670)
The ugly hack around this would be
lambda x: (lambda y: f(y) + g(y))(expensive_computation(x))
Re:Functional Programming in Python [pdf]
by gamegoblin on Saturday July 25, 2015 @01:29AM
(#9946663)
It's possible to hack currying into python with a decorator like:
https://gist.github.com/grantslatton/9221084
(I made this for fun, use at your own risk)
`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.