| Store | Cart

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

From: Steven DAprano <ste...@pearwood.info>
Sun, 10 May 2015 04:16:43 +1000
On Sat, May 09, 2015 at 11:38:38AM -0400, Ron Adam wrote:

> How about an operator for partial?> >           root @ mean @ map $ square(xs)
Apart from the little matter that Guido has said that $ will never be 
used as an operator in Python, what is the association between $ and 
partial?

Most other operators have either been used for centuries e.g. + and - or 
at least decades e.g. * for multiplication because ASCII doesn't have 
the × symbol. The barrier to using a completely arbitrary symbol with no 
association to the function it plays should be considered very high.

I would only support an operator for function composition if it was at 
least close to the standard operators used for function composition in 
other areas. @ at least suggests the ∘ used in mathematics, e.g. 
sin∘cos, but | is used in pipelining languages and shells and could be 
considered, e.g. ls | wc.

My own preference would be to look at @ as the closest available ASCII 
symbol to ∘ and use it for left-to-right composition, and | for 
left-to-right function application. E.g.

(spam @ eggs @ cheese)(arg) is equivalent to spam(eggs(cheese(arg)))

(spam | eggs | cheese)(arg) is equivalent to cheese(eggs(spam(arg)))

also known as compose() and rcompose().

We can read "@" as "of", "spam of eggs of cheese of arg", and | as 
a pipe, "spam(arg) piped to eggs piped to cheese".

It's a pity we can't match the shell syntax and write:

spam(args)|eggs|cheese

but that would have a completely different meaning.


David Beazley has a tutorial on using coroutines in pipelines:

http://www.dabeaz.com/coroutines/

where he ends up writing this:

    f = open("access-log")
    follow(f,
           grep('python',
           printer()))


Coroutines grep() and printer() make up the pipeline. I cannot help but 
feel that the | syntax would be especially powerful for this sort of 
data processing purpose:

    # could this work using some form of function composition?
    follow(f, grep('python')|printer)



-- Steve
_______________________________________________
Python-ideas mailing list
Pyth...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
Recent Messages in this Thread
Ivan Levkivskyi May 06, 2015 01:15 pm
Andrew Barnert via Python-ideas May 06, 2015 01:59 pm
Ivan Levkivskyi May 06, 2015 03:05 pm
Andrew Barnert via Python-ideas May 06, 2015 07:51 pm
Ivan Levkivskyi May 06, 2015 09:38 pm
Stephen J. Turnbull May 07, 2015 01:13 am
Andrew Barnert via Python-ideas May 07, 2015 07:46 am
Ivan Levkivskyi May 07, 2015 05:50 pm
Steven DAprano May 06, 2015 03:48 pm
Guido van Rossum May 06, 2015 04:01 pm
Steven DAprano May 06, 2015 04:17 pm
Andrew Barnert via Python-ideas May 06, 2015 08:40 pm
Florian Bruhin May 06, 2015 08:58 pm
Steven DAprano May 06, 2015 02:51 pm
João Santos May 07, 2015 09:09 am
Chris Angelico May 07, 2015 09:41 am
Rustom Mody May 08, 2015 05:19 am
Ron Adam May 08, 2015 04:05 pm
Chris Angelico May 08, 2015 04:13 pm
Ron Adam May 08, 2015 05:10 pm
Andrew Barnert via Python-ideas May 08, 2015 07:45 pm
Koos Zevenhoven May 08, 2015 07:03 am
Stephen J. Turnbull May 09, 2015 02:58 am
Koos Zevenhoven May 09, 2015 04:00 am
Andrew Barnert via Python-ideas May 09, 2015 07:21 am
Stephen J. Turnbull May 09, 2015 08:36 am
Andrew Barnert via Python-ideas May 09, 2015 10:19 am
Terry Reedy May 09, 2015 03:20 pm
Ron Adam May 09, 2015 03:38 pm
Gregory Salvan May 09, 2015 04:08 pm
Steven DAprano May 09, 2015 06:16 pm
David Mertz May 09, 2015 06:30 pm
Donald Stufft May 09, 2015 06:33 pm
Andrew Barnert via Python-ideas May 09, 2015 10:49 pm
Steven DAprano May 10, 2015 09:31 am
Andrew Barnert via Python-ideas May 09, 2015 10:45 pm
Ron Adam May 10, 2015 03:08 am
Andrew Barnert via Python-ideas May 10, 2015 05:24 am
Ron Adam May 10, 2015 02:45 pm
Koos Zevenhoven May 09, 2015 07:15 pm
Gregory Salvan May 09, 2015 08:41 pm
Gregory Salvan May 09, 2015 10:03 pm
Steven DAprano May 10, 2015 03:14 am
Stephen J. Turnbull May 10, 2015 05:52 pm
Gregory Salvan May 10, 2015 09:11 pm
Steven DAprano May 10, 2015 03:01 am
Douglas La Rocca May 10, 2015 06:40 pm
Koos Zevenhoven May 09, 2015 11:07 pm
Koos Zevenhoven May 10, 2015 08:06 pm
Gregory Salvan May 10, 2015 09:23 pm
Koos Zevenhoven May 10, 2015 09:41 pm
Gregory Salvan May 10, 2015 11:40 pm
Steven DAprano May 10, 2015 02:56 am
Koos Zevenhoven May 10, 2015 03:15 pm
Koos Zevenhoven May 10, 2015 03:30 pm
Messages in this thread