[swift-evolution] Basic element-wise operator set for Arrays, Arrays of Arrays, etc.
David Sweeris
davesweeris at mac.com
Fri Feb 17 13:06:32 CST 2017
> On Feb 17, 2017, at 10:51 AM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
>
> On Fri, Feb 17, 2017 at 12:46 PM, David Sweeris <davesweeris at mac.com <mailto:davesweeris at mac.com>> wrote:
>
>> On Feb 17, 2017, at 10:38 AM, Abe Schneider via swift-evolution <swift-evolution at swift.org <mailto: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.
Yep, that’s what I was demonstrating
> 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!
Oh, sorry, I was confused… I thought the proposal was to allow the syntax.
Well, count me as another +1 for adding a `CoreMath` library (although it should probably be called something else, unless we can make it work in Obj-C, too).
- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170217/53bfb4ed/attachment.html>
More information about the swift-evolution
mailing list