[Python-ideas] Function composition (was no subject)
David Mertz
mertz at gnosis.cx
Sat May 9 20:30:17 CEST 2015
On Sat, May 9, 2015 at 1:16 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, May 09, 2015 at 11:38:38AM -0400, Ron Adam wrote:
>
> > How about an operator for partial?
> >
> > root @ mean @ map $ square(xs)
>
I have trouble seeing the advantage of a special function composition
operator when it is easy to write a general 'compose()' function that can
produce such things easily enough.
E.g. in a white paper I just did for O'Reilly on _Functional Programming in
Python_ I propose this little example implementation:
def compose(*funcs):
"Return a new function s.t. compose(f,g,...)(x) == f(g(...(x)))"
def inner(data, funcs=funcs):
result = data
for f in reversed(funcs):
result = f(result)
return result
return inner
Which we might use as:
RMS = compose(root, mean, square)
result = RMS(my_array)
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150509/dc548e12/attachment.html>
More information about the Python-ideas
mailing list