[swift-evolution] Basic element-wise operator set for Arrays, Arrays of Arrays, etc.

David Sweeris davesweeris at mac.com
Fri Feb 17 12:46:12 CST 2017

> On Feb 17, 2017, at 10:38 AM, Abe Schneider via swift-evolution <swift-evolution at swift.org> wrote:
> If I read Nicolas's post correctly, I think he's more arguing for the
> ability to create syntax that allows Swift to behave in a similar way
> to Numpy/Matlab. While Swift already does allow you to define your own
> operators, the main complaint is that he can't define the specific
> operators he would like.

In Xcode 8.2.1, with the 8.2.1 toolchain, this works (well, it compiles… obviously it doesn’t check for mis-matched array lengths):
infix operator .+
func .+ <T: Integer> (lhs: [T], rhs: [T]) -> [T] {
    return zip(lhs, rhs).map { $0.0 + $0.1 }
print([1,2,3].+[4,5,6]) //outputs [5, 7, 9]

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170217/225b37ef/attachment.html>

More information about the swift-evolution mailing list