[Python-ideas] Function composition (was no subject)

Ron Adam ron3200 at gmail.com
Sat May 9 17:38:38 CEST 2015



On 05/09/2015 03:21 AM, Andrew Barnert via Python-ideas wrote:
>> >I suppose you could write (root @ mean @ (map square)) (xs),

> Actually, you can't. You could write (root @ mean @ partial(map,
> square))(xs), but that's pretty clearly less readable than
> root(mean(map(square, xs))) or root(mean(x*x for x in xs). And that's
> been my main argument: Without a full suite of higher-level operators
> and related syntax, compose alone doesn't do you any good except for toy
> examples.

How about an operator for partial?

           root @ mean @ map $ square(xs)


Actually I'd rather reuse the binary operators.  (I'd be happy if they were 
just methods on bytes objects BTW.)

           compose(root, mean, map(square, xs))

           root ^ mean ^ map & square (xs)

           root ^ mean ^ map & square ^ xs ()

Read this as...

          compose root, of mean, of map with square, of xs

Or...

           apply(map(square, xs), mean, root)

           map & square | mean | root (xs)

           xs | map & square | mean | root ()


Read this as...

           apply xs, to map with square, to mean, to root


These are kind of cool, but does it make python code easier to read?  That 
seems like it may be subjective depending on the amount of programming 
experience someone has.

Cheers,
    Ron






More information about the Python-ideas mailing list