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

Gregory Salvan apieum at gmail.com
Sun May 10 00:03:24 CEST 2015


Nobody convinced by arrow operator ?

like: arg -> spam -> eggs -> cheese
or cheese <- eggs <- spam <- arg

This also make sense with annotations:

def func(x:type1, y:type2) -> type3:
    pass

we expect func to return type3(func(x, y))



2015-05-09 22:41 GMT+02:00 Gregory Salvan <apieum at gmail.com>:

> pipeline operator may be confusing with bitwise operator.
> In this case :
> eggs = arg | spam | cheese
>
> Is eggs a composed function or string of bits ?
>
>
> 2015-05-09 21:15 GMT+02:00 Koos Zevenhoven <koos.zevenhoven at aalto.fi>:
>
>>
>> On 2015-05-09 21:16, Steven D'Aprano wrote:
>>
>>> 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".
>>>
>>
>> For me these are by far the most logical ones too, for exactly the same
>> reasons (and because of the connection of @ with matrix multiplication and
>> operators that operate from the left).
>>
>>  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.
>>>
>>>
>>
>> But it does not need to have a different meaning. You could in addition
>> have:
>>
>> spam @ eggs @ cheese @ arg   #  equivalent to spam(eggs(cheese(arg)))
>>
>> arg | spam | eggs | cheese    # equivalent to cheese(eggs(spam(arg)))
>>
>> Here, arg would thus be recognized as not a function.
>>
>> In this version, your example of spam(args)|eggs|cheese would do exactly
>> the same operation as (spam | eggs | cheese)(args) :-).
>>
>>
>>  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)
>>>
>>>
>>>
>>>
>> This seems promising!
>>
>>
>> -- Koos
>>
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150510/28942e60/attachment.html>


More information about the Python-ideas mailing list