Uniform Function Call Syntax (UFCS)

Chris Angelico rosuav at gmail.com
Sun Jun 8 23:44:22 EDT 2014


On Mon, Jun 9, 2014 at 1:20 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Mon, 09 Jun 2014 03:10:03 +1000, Chris Angelico wrote:
> [...]
>> Stdio.write_file("foo.png",Image.PNG.encode(Image.JPEG.decode(
>> Stdio.read_file("foo.jpg")).autocrop().rotate(0.5).grey()));
>>
>> With UFCS, that could become perfect data flow:
>>
>> read_file("foo.jpg").JPEG_decode().autocrop().rotate(0.5).grey()
>> .PNG_encode().write_file("foo.png");
>>
>> I had to solve the syntactic ambiguity here by importing all the
>> appropriate names
>
> I'm not sure how this is *syntactic* ambiguity.

The ambiguity I'm talking about here is with the dot. The original
version has "Stdio.read_file" as the first function called; for a
Python equivalent, imagine a string processing pipeline and having
"re.sub" in the middle of it. You can't take "re.sub" as the name of
an attribute on a string without some fiddling around that completely
destroys the point of data-flow syntax. So I cheated, and turned
everything into local (imported) names (adorning the ones that needed
it). This is a bad idea in Pike for the same reason it's a bad idea in
Python - you end up with a massively polluted global namespace.

This could be solved, though, by having a completely different symbol
that means "the thing on my left is actually the first positional
parameter in the function call on my right", such as in your example:

> plus(1, 2) | divide(2)

This would be absolutely identical to:

divide(plus(1, 2), 2)

Maybe you could even make it so that:

plus(1, 2) x=| divide(y=2)

is equivalent to

divide(x=plus(1, 2), y=2)

for the sake of consistency, and to allow the pipeline to inject
something someplace other than the first argument.

I'm not sure whether it'd be as useful in practice, though. It would
depend partly on the exact syntax used. Obviously the pipe itself
can't be used as it already means bitwise or, and this needs to be
really REALLY clear about what's going on. But a data-flow notation
would be of value in theory, at least.

ChrisA



More information about the Python-list mailing list