evhub / coconut

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

Numpy array literals #433

Open ArneBachmann opened 6 years ago

ArneBachmann commented 6 years ago

What about allowing in-place operations for tuples like so:

(a, b) += (c, d)  # equivalent to a+= c; b +=d
A = (1, 2, 3)
B = (2, 3, 4)
C = A * B  # looks a bit like matrix manipulation but isn't
print(C)  # (2, 6, 12)

I guess the problem is how to define the semantics, as many different behaviour are imaginable (like += for first element but /= for second). Also it's nothing Python-typical other than allowing tuple-assignment like (a, b) = (b * 2, a +1)

evhub commented 1 year ago

If you want this, you should just use a numpy array. We have multidimensional array literals, but they don't yield numpy arrays by default unless fed them. Perhaps a n[1, 2;; 3, 4] syntax for a numpy array literal would be desirable here?

evhub commented 1 year ago

The problem with n[...] is that it conflicts with indexing the variable n. If we want to do this, we need some other syntax for this.

Also, if you want the n[...] syntax for numpy arrays to work, it's pretty easy to get it yourself via:

operator n
from numpy import asarray as (n)
n[1, 2] |> print