[swift-evolution] Spread Operator as Shorthand for Map

Al Skipp al_skipp at fastmail.fm
Wed Dec 16 09:23:22 CST 2015


> On 16 Dec 2015, at 12:47, Sean Kosanovich via swift-evolution <swift-evolution at swift.org> wrote:
> 
> As a Groovy user, I really enjoy the shorthand Spread Operator over the Collect Closure (Map is the equivalent in Swift).
> 
> Consider the following Swift code:
> 
> struct Car {
>     let make: String
>     let model: String
> }
> 
> let cars = [Car(make: "Jeep", model: "Grand Cherokee"), Car(make: "Dodge", model: "Challenger")]
> let makes = cars.map() { $0.make }
> 
>  
> 
> Now consider the same code using a Spread Operator:
> 
> let makes = cars*.make

I think there is a tension in Swift between its object oriented nature and higher-order functions which leads to a lot of repetition of { $0.property }

In this example:
cars.map { $0.make }

I’d like to write the following, but ‘make’ is a member, not a free function:
cars.map(make)

Is anyone else irked by the preponderance of { $0.property } in Swift (or is it just me being grumpy)? Not sure what the solution would be, but I can’t say I’m keen on the suggestion of:

cars*.make

It’s too mysterious.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/54bdb8dd/attachment.html>


More information about the swift-evolution mailing list