Pipe Function in Python
In [1]:
from functools import reduce
This pipe function was inspired by this JavaScript-related tweet:
In [2]:
%%html
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Reduce is versatile:<br>const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);</p>— Eric Elliott (@_ericelliott) <a href="https://twitter.com/_ericelliott/status/1111737323518156802?ref_src=twsrc%5Etfw">March 29, 2019</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
In [3]:
def pipe(item, *funcs):
r"""Combine the outputs an arbitrary number of functions.
>>> attrs = (
... 'strip',
... 'upper',
... )
>>> funcs = (get_func(attr) for attr in attrs)
>>> pipe(' hello, world \t\n ', *(*funcs, exclaim))
'HELLO, WORLD!'
"""
def handler(value, f):
return f(value)
return reduce(handler, funcs, item)
In [4]:
def get_func(attr: str):
"""Return a function that returns the result of calling a callable attribute on an item.
>>> get_func('upper')('hello')
'HELLO'
"""
def func(item):
return getattr(item, attr)()
return func
In [5]:
def exclaim(s: str):
"""Append an exclaimation point to the end of a string.
>>> exclaim('hello')
'hello!'
"""
return f"{s}!"
In [6]:
if __name__ == "__main__":
import doctest
import sys
sys.argv[-1] = "-v" # Make doctest.testmod verbose
doctest.testmod()