[Python-ideas] add fluent operator to everything
Robert Vanden Eynde
robertve92 at gmail.com
Tue Feb 19 17:07:36 EST 2019
Heyy, it's funcoperators idea !
>>> [1,2,3].append(4)::sort()::max() +1
[1, 2, 3] |append(4) |to(sorted) |to(max) |to(plus1)
You just have to :
pip install funcoperators
from funcoperators import postfix as to
plus1 = postfix(lambda x: x+1)
from funcoperators import postfix
def append(x):
return postfix(lambda L: L + [x])
The module also propose a way to have infix operators, so if you want to
have the "::" operator that have the same precedence than "/", you could
call it "/ddot/" and do :
[1,2,3] /ddot/ append(4) /ddot/ sort /ddot/ max +1
But for your case (creating Bash like "pipes" the above solution seems
better).
https://pypi.org/project/funcoperators/
PS: you could create "modify and return" version of "append" or "sort" like
this :
def append(x):
@postfix
def new(L):
L.append(x)
return L
return new
Then you'd have :
>>> a = [1,2,3]
>>> b = a |append(5) |append(6)
>>> a is b
True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190219/d6511431/attachment.html>
More information about the Python-ideas
mailing list