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

Xiaodi Wu xiaodi.wu at gmail.com
Fri Feb 17 12:51:09 CST 2017


On Fri, Feb 17, 2017 at 12:46 PM, David Sweeris <davesweeris at mac.com> wrote:

>
> 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] {
>

precondition(lhs.count == rhs.count)


>     return zip(lhs, rhs).map { $0.0 + $0.1 }
> }
> print([1,2,3].+[4,5,6]) //outputs [5, 7, 9]
>

There's nothing, afaik, which stands in the way of that syntax today. The
proposal is to extend the standard library to add syntax for a math
library. The idea of having a core math library has already been mentioned
on this list, to great approval, but it should come in the form of an
actual library, and not a syntax only!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170217/cd23ccfe/attachment.html>


More information about the swift-evolution mailing list