[swift-evolution] Spread Operator as Shorthand for Map

Thorsten Seitz tseitz42 at icloud.com
Mon Dec 28 11:10:37 CST 2015


+1 for being able to use cars.map(make)

> Am 16.12.2015 um 16:23 schrieb Al Skipp via swift-evolution <swift-evolution at swift.org>:
> 
>> 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.
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151228/f41ae574/attachment.html>


More information about the swift-evolution mailing list