On Wed, Mar 16, 2016, at 10:57, Steven D'Aprano wrote:
> For instance, we can take a string, extract all the digits, convert them
> to
> ints, and finally multiply the digits to give a final result:
> 
> py> from operator import mul
> py> "abcd12345xyz" | Filter(str.isdigit) | Map(int) | Reduce(mul)
> 120

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)
pfilter = partial(partial, filter)
pmap = partial(partial, map)
preduce = partial(partial, reduce)

fpipe("abcd12345xyz", pfilter(str.isdigit), pmap(int), preduce(mul))

> (For the definitions of Filter, Map and Reduce, see the code at the
> ActiveState recipe, linked above). In my opinion, this is much nicer
> looking that the standard Python `filter`, `map` and `reduce`:
> 
> py> reduce(mul, map(int, filter(str.isdigit, "abcd12345xyz")))
> 120
> 
> as this requires the operations to be written in the opposite order to
> the
> order that they are applied.
> 
> 
> 
> -- 
> Steven
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to