[swift-evolution] [Pitch] Add `mapValues` method to Dictionary

Nate Cook nate at natecook.com
Thu May 26 16:37:45 CDT 2016


> On May 26, 2016, at 4:23 PM, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> on Tue May 24 2016, Nate Cook <swift-evolution at swift.org> wrote:
> 
>>> On May 24, 2016, at 7:43 AM, Matthew Johnson via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> Sent from my iPad
>>> 
>>>> On May 24, 2016, at 12:59 AM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> 
>>>>> I have a small remark though, wouldn’t it be better to let transform be of type (Key, Value) throws -> T instead of (Value) throws -> T? You can just ignore the key (with _) if you don’t need it, but I think it might come in handy in some cases.
>>>> 
>>>> The problem is, that closes the door to writing many simple maps in functional style. For instance, this:
>>>> 
>>>>  dictionaryOfNumbers.mapValues(abs)
>>>> 
>>>> Would have to become this:
>>>> 
>>>>  dictionaryOfNumbers.mapValues { _, v in abs(v) }
>>>> 
>>>> (It *might* be possible to do it with `$1`, but I'm not sure; there are some limitations around that.)
>>>> 
>>>> A value-value map is just simpler and cleaner, while almost always giving you what you need.
>>> 
>>> +1.  
>>> 
>>> I don't think I have ever mapped keys.  Incidentally, that doesn't have the usual semantics of a map operation as you can produce duplicate keys.
>> 
>> I think this conversation suffers from not knowing whether Dictionary
>> is going to receive a sequence-based initializer. There's been a
>> proposal for that in the PR queue for quite a while. Could someone in
>> the core team please either merge or close that proposal?
>> 
>> https://github.com/apple/swift-evolution/pull/125
> 
> Done.  I was just waiting until we could assign it SE-0100.
> Congratulations! ;-)

💯😂

>> If there's no sequence-based init, mapValues should probably produce a
>> new Dictionary. If there is, having mapValues produce a dictionary
>> would be redundant. I'd rather see a MapDictionary type that wraps a
>> dictionary and only evaluates the transformation on access, rather
>> than eagerly allocating storage and performing all the
>> transformations.
>> 
>> You can take a look at a rudimentary MapDictionary in this gist:
>> https://gist.github.com/natecook1000/7cfc5c04f18237066f7aca6e9658f276
>> 
>> Best,
>> Nate
>> 
>>>> -- 
>>>> Brent Royal-Gordon
>>>> Architechies
>>>> 
>>>> _______________________________________________
>>>> 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
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list