evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.09k stars 125 forks source link

Allow custom in-place-operators #729

Open yggdr opened 1 year ago

yggdr commented 1 year ago

Currently we have the possibility to create our own custom operators like so:

operator ~~
def left ~~ right = dostuff(right) - dostuff(left)

but we don't seem to have a possibility to create in-place versions. I propose an additional language-construct like

in-place-operator ~~=

that takes an already defined operator ~~ (like above) and creates new functionality allowing left ~~= right to be syntactic sugar for left = left ~~ right.

evhub commented 1 year ago

You can always do

operator ~~
(~~) = (,)
x = 1
x |>= (. ~~ 2)
assert x == (1, 2)

which even compiles down to remove the partial and give you the exact code you'd want for an in-place operator:

_coconut_op_U7e_U7e = (_coconut_comma_op)
x = 1
x = (_coconut_op_U7e_U7e)(x, 2)
assert x == (1, 2)
soupierre commented 7 months ago

Hello, I am going to try and do that.