Bash-like pipes in Python

Random832 random832 at fastmail.com
Wed Mar 16 11:29:54 EDT 2016


On Wed, Mar 16, 2016, at 11:20, Random832 wrote:
> How about:
> 
> from functools import partial, reduce
> from operator import mul
> def rcall(arg, func): return func(arg)
> def fpipe(*args): return reduce(rcall, args)

It occurs to me that this suggests a further refinement: have all
functions (and classes? map and filter seem to be classes.) define
__ror__ as calling the left-hand operand. Then this could be written as
"abcd12345xyz" | pfilter(str.isdigit) | pmap(int) | preduce(mul).

You could also define, say, __mul__ as partial application, so you could
write "abcd12345xyz" | filter*str.isdigit | map*int | reduce*mul.

> pfilter = partial(partial, filter)
> pmap = partial(partial, map)
> preduce = partial(partial, reduce)
> 
> fpipe("abcd12345xyz", pfilter(str.isdigit), pmap(int), preduce(mul))



More information about the Python-list mailing list