[swift-evolution] [Pitch] Add `mapValues` method to Dictionary
Dan Appel
dan.appel00 at gmail.com
Sun May 22 01:47:58 CDT 2016
I don't understand - why do we need a map protocol for a lazy mapvalues?
On Sat, May 21, 2016 at 11:12 PM Brent Royal-Gordon <brent at architechies.com>
wrote:
> > Obviously if it's required to have a lazy variant for all collection
> methods, I'd probably pull back this proposal, as it would automatically be
> rejected by the core team.
>
> I'm not a member of the core team, but I doubt it's necessary.
>
> I don't think it makes much sense to wait for a lazy implementation. If
> there *is* going to be a Map protocol, then the best way to implement it
> will require an advanced generics feature that's still in the proposal
> stages:
>
> // Note: I'm simplifying signatures here.
>
> protocol Map: Collection {
> associatedtype Key
> associatedtype Value
> associatedtype Iterator: IteratorProtocol where
> Iterator.Element == (Key, Value)
>
> subscript (key: Key) -> Value? { get }
> func mapValues<T>(transform: Value -> T) -> [Key: T]
> }
>
> And implementing the laziness will similarly work best with not only that
> feature, but also conditional conformances:
>
> protocol LazyMap: Map, LazyCollectionProtocol {
> subscript (key: Base.Key) -> Base.Value? { get }
> func mapValues<T>(transform: Value -> T)(…) ->
> LazyMappedValuesMap<Base, T>
> }
>
> extension LazyCollection: LazyMap where Base: Map {
> …
> }
>
> struct LazyMapValuesMap<Base: Map, NewValue>: Map {
> …
> }
>
> If we *don't* wait, on the other hand, we're going to end up in
> manual-specification and GYB hell. (And even then, I believe conditional
> conformances are not enough to force all LazyCollectionProtocol conformers
> to support Map if they have a Map base. You'd need conditional *conformance
> requirements*, which are *not* planned because it's not safe to add a
> requirement to a protocol.)
>
> I just don't think laziness is so crucial that we should stop the presses
> until we have it. `mapValues` carries water all by itself, even without a
> matching `lazy.mapValues`.
>
> --
> Brent Royal-Gordon
> Architechies
>
> --
Dan Appel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160522/79ac53a7/attachment.html>
More information about the swift-evolution
mailing list