[Python-ideas] How do you think about these language extensions?

Steven D'Aprano steve at pearwood.info
Fri Aug 18 08:06:10 EDT 2017


Hello Thautwarm, and welcome!


Sorry for the delay in responding, but this has been a very busy week 
for me personally, and an even busier week for my inbox, and so I missed 
your post until now.

On Sun, Aug 13, 2017 at 12:49:45PM +0000, 王宣 ? wrote:
> 
> Hi all,
> 
> I've just finished a language extension for CPython 3.6.x to support 
> some additional grammars like Pattern Matching. And It's compatible 
> with CPython.

It is really good to see some actual practical experiments for these 
features, rather than just talking about them. Thank you!


[...]
> # where syntax
> 
>     from math import pi
>     r = 1  # the radius
>     h = 10 # the height
>     S = (2*S_top + S_side) where:
>         S_top  = pi*r**2
>         S_side = C * h where:
>             C = 2*pi*r

This has been suggested a few times. The first time, I disliked it, but 
I've come across to seeing its value. I like it.

I wonder: could we make the "where" clause delay evaluation until the 
entire block was compiled, so that we could write something like this:

    S = (2*S_top + S_side) where:
        S_top  = pi*r**2
        S_side = C * h  # C is defined further on
        C = 2*pi*r

That's more how "where" is used mathematically.


> # lambda&curry :
> 
>     lambda x: lambda y: lambda z: ret where:
>          ret = x+y
>          ret -= z
>     .x -> .y -> .z -> ret where:
>          ret = x+y
>          ret -= z
>     as-with x def as y def as z def ret where:
>          ret = x+y
>          ret -= z

I'm afraid I can't make heads or tails of that. Apart from guessing that 
it creates a function, I have no idea what it would do.



> # arrow transform (to avoid endless parentheses and try to be more readable.
> 
>   >> range(5) -> map(.x->x+2, _) -> list(_)
>   >> [2,3,4,5,6]

I like the idea of chained function calls, like pipes in shell 
languages such as bash. I've written a proof-of-concept for that:

http://code.activestate.com/recipes/580625-collection-pipeline-in-python/

I prefer | to -> but that's just a personal preference.

I don't like the use of _ in there. Underscore already has a number of 
special meanings, such as:

- a convention for "don't care"

- in the interactive interpreter, the last value calculated

- used for internationalisation

I don't think that giving _ yet another special meaning, and this one 
built in to the language, is a good idea.



> # pattern matching use "condic" as keyword is for avoiding the 
> # conflictions against the standard libraries and packages from third 
> # party. "switch" and "match" both lead to conflictions.

This is a hard problem to deal with, but "condic" sounds awful. What is 
is supposed to mean? Short for "condition"?


>     condic+(type) 1:
>         case a:int => assert a == 1 and type(a) == 1
>     [>]
>         case 0     => assert 1 > 0
>     [is not]
>         case 1     => assert 1 is not 1
>     otherwise  => print("nothing")
> 
>     condic+() [1,2,3]:
>         case (a,*b)->b:list => sum(b)
>         +[]
>         case  []            => print('empty list')
>         +[==]
>         case  (a,b):(1,2)   => print("the list is [1,2]")

I don't know how to read those.


[...]
> Here is an example to use flowpython, which gives the permutations of a sequence.
> 
>     from copy import deepcopy
>     permutations = .seq -> seq_seq where:
>         condic+[] seq:
>             case (a,  ) => seq_seq = [a,]
>             case (a, b) => seq_seq = [[a,b],[b,a]]
>             case (a,*b) =>
>                 seq_seq = permutations(b) -> map(.x -> insertAll(x, a),  _) -> sum(_, []) where:
>                      insertAll = . x, a -> ret where:
>                          ret = [ deepcopy(x) -> _.insert(i, a) or _ for i in  (len(x) -> range(_+1))  ]

I find that almost unreadable. Too many new features all at once, it's 
like trying to read a completely unfamiliar language.

How would you translate that into regular Python?



Thanks for your experiments!



-- 
Steve


More information about the Python-ideas mailing list