[Python-ideas] How do you think about these language extensions?
王宣 ?
twshere at outlook.com
Sun Aug 13 08:49:45 EDT 2017
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.
I'm looking for constructive advice, and I wonder if you will be interested in this one.
（ the project address is https://github.com/thautwarm/flowpython)
[https://avatars1.githubusercontent.com/u/22536460?v=4&s=400]<https://github.com/thautwarm/flowpython>
thautwarm/flowpython<https://github.com/thautwarm/flowpython>
github.com
flowpython - tasty feature extensions for python(python3).
Some examples here:
# 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
# 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
# arrow transform (to avoid endless parentheses and try to be more readable.
>> range(5) -> map(.x->x+2, _) -> list(_)
>> [2,3,4,5,6]
# 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.
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]")
The grammars with more details and examples can be found in
https://github.com/thautwarm/flowpython/wiki
Does it interest you? If so, you can try it if you have CPython 3.6.x.
pip install flowpython
python -m flowpython -m enable/disable
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)) ]
If the object permutations are defined, try these codes in console:
>> range(3) -> permutations(_)
>> [[0, 1, 2], [1, 0, 2], [1, 2, 0], [0, 2, 1], [2, 0, 1], [2, 1, 0]]
Does it seem to be interesting?
Thanks,
Thautwarm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170813/30eb7a1f/attachment.html>
More information about the Python-ideas
mailing list