[swift-evolution] Pitch: Support for map and flatMap with smart key paths
Dave Abrahams
dabrahams at apple.com
Fri Jun 9 22:29:14 CDT 2017
on Fri Jun 09 2017, Saagar Jha <swift-evolution at swift.org> wrote:
> It reads better and feels more natural. In my mind, it’s similar to the difference between
>
> ["1", "2", "3"].flatMap { Double($0) }
>
> and
>
> ["1", "2", "3"].flatMap(Double.init)
>
> Saagar Jha
I am not convinced this syntactic sugar is worth complicating the
language or library for, but if it is, IMO the right thing is to make a
keypath be-a subtype of the appropriate function type, rather than to
start down the path of creating a keypath overload for every method that
takes a closure argument.
>
>> On Jun 9, 2017, at 16:06, Max Moiseev via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> Sorry. I might be missing something. Why is this better than:
>>
>> let allEmployees = Set(managers.flatMap { $0.directReports }
>>
>> ?
>>
>>> On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution
>>> <swift-evolution at swift.org> wrote:
>>>
>>> The new smart key path feature is really lovely, and feels like a great addition to Swift.
>>>
>>> It seems like it might be straightforward to add overloads of `map`
>>> and `flatMap` to the standard library to make use of the new
>>> functionality:
>>>
>>> let managers = flatOrganisation.managers
>>> let allEmployees = Set(managers.flatMap(\.directReports))
>>> let employeeNames = Set(allEmployees.map(\.name))
>>>
>>> This feels like a really natural way of working with key paths in a
>>> functional style. It makes a lot of sense for collections, and
>>> possibly for Optional too (although as far as I can see optional
>>> chaining is more or less equivalent, and with more compact syntax).
>>>
>>> I’m hoping that this might be low-hanging fruit that could be
>>> considered for the Swift 4 release. I’d be happy to have a go at
>>> writing a proposal if there’s interest!
>>>
>>> –Adam
>>>
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
--
-Dave
More information about the swift-evolution
mailing list