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

Tim Vermeulen tvermeulen at me.com
Wed May 25 03:34:44 CDT 2016


> 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.

As has been pointed out in other messages here, the slight change I suggested would let you see the key when mapping the value, but it wouldn’t let you transform the key. Either way, if Swift 3 gives us a dictionary initialiser from a sequence, then this change won’t be necessary. If it won’t, I still think seeing the key could come in handy at times (although I can’t think of an example).

> On 24 May 2016, at 14:43, Matthew Johnson <matthew at anandabits.com> 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.
> 
>> 
>> -- 
>> Brent Royal-Gordon
>> Architechies
>> 
>> _______________________________________________
>> 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